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?
You 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With