Cookie,用于保存包括一个变量在内的多个值
Cookie to hold multiple values including a variable?
我正试图将多个变量保存在一个cookie中,这是代码:
function changeColors(){
//get the numbers from the html
var rd = parseInt(document.getElementById("red").value);
var gr = parseInt(document.getElementById("green").value);
var bl = parseInt(document.getElementById("blue").value);
var op = parseFloat(document.getElementById("opacity").value);
//convert the decimal into hexadecimal
var rdhex = (rd < 16) ? "0" + rd.toString(16) : rd.toString(16);
var grhex = (gr < 16) ? "0" + gr.toString(16) : gr.toString(16);
var blhex = (bl < 16) ? "0" + bl.toString(16) : bl.toString(16);
//concatenate all hex to generate a color
var hexcode = "#" + rdhex + grhex + blhex;
//view the change in the browser
document.getElementById("div").style.backgroundColor = hexcode;
document.getElementById("colordisplay").innerHTML = hexcode;
//change opacity
document.getElementById("div").style.opacity = op;
}
function WriteCookie()
{
if( document.myform.name.value == "" ){
alert("Enter some value!");
return;
}
cookievalue= escape(document.myform.red.value) + ";" +
escape(document.myform.red.value)+ ";"+
escape(document.myform.green.value)+ ";"+
escape(document.myform.blue.value)+ ";"+
escape(document.myform.opacity.value)+ ";";
document.cookie="color=" + cookievalue;
alert("Setting Cookies : " + "color=" + cookievalue );
// To revise!!!How do we set multiple values to one cookie?
function ReadCookie()
{
var allcookies = document.cookie;
alert("All Cookies : " + allcookies );
// Get all the cookies pairs in an array
cookiearray = allcookies.split(';');
我需要将名称、RGB和不透明度存储在cookie中,以便稍后用户可以从他自己管理的以前的颜色中进行选择。我的问题是如何在cookie中存储var六进制代码和不透明度以及颜色名称?
您可以将值与join()
连接在一起,即:
var separator = ":" // or whatever
var values = [name, rgb, opacity].join(separator);
// then you store the values in a cookie
document.cookie = "myValues=" + values;
要在cookie中存储值,请参阅What is the";最好的";使用JavaScript 获取和设置单个cookie值的方法
相关文章:
- 将函数的上下文应用于javascript变量
- 将文本框中的值用于按钮窗体操作上的变量
- AngularJS-使模型变量可用于jQuery
- 用于交换两个变量的 JavaScript 函数
- 使变量可用于不带闭包的异步调用
- 用于提取基于点概念的变量的动态Regex
- 保留 JQuery Vars 用于另一个函数(全局变量?)
- 用于添加和删除类的 Jquery 打开/关闭变量
- 用于比较 HTML 标记内的变量的正则表达式
- 流星技术/模式,用于等待数据库变量更改,然后在 in 之后执行某些操作
- 转义序列jQuery,用于在引号内使用变量
- 将变量用于 jquery CSS 属性名称
- 将 rails 实例变量用于 javascript 条件
- 如何将 js 变量用于另一个文件
- 不能将 jQuery 中定义的变量用于 html
- PHP数组中的javascript变量用于jquery自动完成
- Javascript在不同的函数中引入变量;用于谷歌投标的天气脚本
- 我们可以将一个.js文件的变量用于其他.js文件吗
- 在 JavaScript 中使用 PHP 变量用于 Google 地图多边形
- 一个变量用于 JS 中的两个函数