low tech
07-13-2011, 11:07 AM
Hi all
How should I be doing this?
I am trying to 'learn' how to set more than one value in a cookie.
Then get them back out. Obviously i'm on the wrong track.
These are my 'test' values:
var val = document.getElementById("yrname").value; //peterPan
var val1 = document.getElementById("password").value; //tinkerBell
//send to create cookie factory
createCookie("inbed", val + '~' + val1, "1");
Then from url : alert(cookie)
I get:
inbed=peterPan~tinkerBell
So far so good (well what I expected anyway)
now using this function I completely lose tinkerBell.
she was inbed with peterPan.
function readCookie(name) //name is passed in
{
var cookies = document.cookie.split(/~/g); //split at ~
//alert(cookies);
//results:
//cookies: inbed=peterPan,tinkerBell
//cookies[0]: mytestcookie=peterPan
//cookies[1]: tinkerBell //the last sighting
var arg = name + "=";
for ( var c = 0; c < cookies.length; ++c ) //iterate through the cookie
{
var ck = cookies[c]; //cookie array into an var
//alert(ck); //inbed=peterPan (no tinkerBell -- not what I expected!
//basically i'm now lost!!!
//I don't get what happened to tinkerBell.
if ( ck.indexOf(arg) == 0 )
//if the array at the index of arg 'name=' equal to position 0.
{
var temp = ck.split(/=/); //split the cookie array at =
//alert(temp); //inbed=peterPan
//alert(temp[0]); //inbed
//alert(temp[1]); //peterPan
,
return decodeURI(temp[1]) //bring back array 1
}
}
return "";
}help on how I should
be doing this
much appreciated
LT:)
How should I be doing this?
I am trying to 'learn' how to set more than one value in a cookie.
Then get them back out. Obviously i'm on the wrong track.
These are my 'test' values:
var val = document.getElementById("yrname").value; //peterPan
var val1 = document.getElementById("password").value; //tinkerBell
//send to create cookie factory
createCookie("inbed", val + '~' + val1, "1");
Then from url : alert(cookie)
I get:
inbed=peterPan~tinkerBell
So far so good (well what I expected anyway)
now using this function I completely lose tinkerBell.
she was inbed with peterPan.
function readCookie(name) //name is passed in
{
var cookies = document.cookie.split(/~/g); //split at ~
//alert(cookies);
//results:
//cookies: inbed=peterPan,tinkerBell
//cookies[0]: mytestcookie=peterPan
//cookies[1]: tinkerBell //the last sighting
var arg = name + "=";
for ( var c = 0; c < cookies.length; ++c ) //iterate through the cookie
{
var ck = cookies[c]; //cookie array into an var
//alert(ck); //inbed=peterPan (no tinkerBell -- not what I expected!
//basically i'm now lost!!!
//I don't get what happened to tinkerBell.
if ( ck.indexOf(arg) == 0 )
//if the array at the index of arg 'name=' equal to position 0.
{
var temp = ck.split(/=/); //split the cookie array at =
//alert(temp); //inbed=peterPan
//alert(temp[0]); //inbed
//alert(temp[1]); //peterPan
,
return decodeURI(temp[1]) //bring back array 1
}
}
return "";
}help on how I should
be doing this
much appreciated
LT:)