I have a problem with javascript cookies that I have been struggling with for days.
I have this function(almost the same from w3):
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x.indexOf(c_name)==0)
{
ar[i]=y;
arr[i]=x;
}
}
}
Lets say that I have stored two cookies with the following names:tablet1 and taskt1. When I call getCookie('table'), ar.length is 1. After that I run getCookie('task') and ar.length is 2. This causes me a lot of problems. If I put ar=0; at the start of the function nothing works, it’s like the function just stores values every time I call it without erasing the old ones.
============================
var arrCookies = {};
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x.indexOf(c_name)==0)
{
arrCookies[c_name] = y;
}
}
}
when i do that the arrCookies[c_name] it does not store any values–>arrCookies[c_name].lenght is zero
=================================================
i also did
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x.indexOf(c_name)==0)
{
if(c_name=='table'){
xxx[i]=x;
xx[i]=y;
}
else if(c_name=='task'){
arr[i]=x;
ar[i]=y;
}
}
}
}
and i get the same results with my first script
instead of using ar[i] in the loop, use ar[j] like this:
do you see the mistake in your code?