admin管理员组文章数量:1325408
I am trying to save multiple variables in a cookie and have and this is the code:
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(';');
I need to store the Name, RGB and opacity in a cookie so that later on the user can choose from previous colors that he has managed himself. My question is how can I store the var hexcode and opacity together with the name of the color in a cookie?
I am trying to save multiple variables in a cookie and have and this is the code:
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(';');
I need to store the Name, RGB and opacity in a cookie so that later on the user can choose from previous colors that he has managed himself. My question is how can I store the var hexcode and opacity together with the name of the color in a cookie?
Share Improve this question asked Dec 14, 2013 at 18:32 Henry LynxHenry Lynx 1,1291 gold badge20 silver badges42 bronze badges1 Answer
Reset to default 7You can join the values together with join()
, i.e.:
var separator = ":" // or whatever
var values = [name, rgb, opacity].join(separator);
// then you store the values in a cookie
document.cookie = "myValues=" + values;
For storing values in cookies see What is the "best" way to get and set a single cookie value using JavaScript
本文标签: javascriptCookie to hold multiple values including a variableStack Overflow
版权声明:本文标题:javascript - Cookie to hold multiple values including a variable? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742130104a2422119.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论