I can't tell exactly what you are doing, but I'll start from the beginning for reading cookies.
document.cookie should contain a list of cookies separated by semi-colons. Each cookie should be a name=value pair. So alert(document.cookie) would give something like cookie1=value1;cookie2=value2;cookie3=value3 and so on.
So in order to read a value cookies, it's a good idea to split the document.cookie string into pieces. Then you can iterate though the list of cookies and split them down into their name and value pairs. You should not unescape the cookie value until after you have split it up-- otherwise, you could run into problems if your cookie value contains a semi-colon or an equals sign.
Here is a function which gets all of the cookies and returns them in a nice associative array:
Code:
function getCookies(){
var myCookies = {};
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var crumbs = cookies[i].split('=');
var name = unescape(crumbs[0]).replace(/^\s+|\s+$/g, ''); // trim any whitespace
var value = unescape(crumbs[1]).replace(/^\s+|\s+$/g, ''); // trim any whitespace
myCookies[name] = value;
}
return myCookies;
}
// example usage to read a cookie:
var cookies = getCookies();
alert(cookies['my_cookie_name']);
__________________
The interlocking pieces of web development: usability, performance, accessibility, and standards.
|