PDA

View Full Version : Looping through cookie values?!?!?


Syy
02-28-2006, 10:17 AM
Hi All

I have a bit of a problem, i am creating a cookie and adding content not a problem, but when i try and retrieve the content i am having the problem,

basically i have the following values in my cookie

sections:3&region1:London&nation1:England&region2:Manchester&nation2:England&region3:Glasgow&nation3:Scotland

the regions and nations could be empty and could go up to 10 or more i.e region10: and nation10:
the sections value will always relate to how many i have - so in this example there are 3

so what i was trying to do was a simple for loop and add the value onto the end like this

for(i=0;i<sections;i++){
alert(cookie.region[i]);
alert(cookie.nation[i]);
}


but it tells me
Error: cookie.region has no properties

Can anyone help me loop through this?

konithomimo
02-28-2006, 01:18 PM
Take that string and send it to the following function:
<script type="text/javascript">
function createMyArrays(mystring)
{
var sets = mystring.split('&');
var cls = new Array();
var vals = new Array();
var regions = new Array();
var nations = new Array();
var i;
var j = k = 0;
for(i=0;i<sets.length;i++)
{
cls[i]=sets[i].split(':')[0];
vals[i]=sets[i].split(':')[1];
}
for(i=0;i<cls.length;i++){
if(cls[i].match('region'))
{
regions[j]=vals[i];
j++;
}
if(cls[i].match('nation'))
{
nations[k]=vals[i];
k++;
}
}

alert(regions[2]);
alert(nations[2]);
}
</script>

As an example it will display the third region/nation. Then just do whatever you want to make it do what you want.