I’m handling cookies using JavaScript to store some values in my asp.net web application.
I use document.cookie to save some values (converted into a lengthy string). But i want that value to be accessible across all the pages in my application.
When i try to get that value from a different page, i get the values pertaining to the document in the current URL.
In short i save the value in the cookie in http://myapp/doc1.aspx and want to retrieve it in http://myapp/doc2.aspx
So is document.cookie is pertaining to a single document scope? How can i save/read cookies across the site?
Update
This is how i get and set cookies
function getCookie(c_name)
{
try{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
}
catch(e)
{}
return "";
}
function setCookie ( name, value, exp_d)
{
var cookie_string = name + "=" + escape ( value );
if ( exp_d )
{
var exdate=new Date();
var expires = new Date ( exdate.getYear(), exdate.getMonth(), exdate.getDay()+exp_d );
cookie_string += "; expires=" + expires.toGMTString();
}
document.cookie = cookie_string;
}
But i’m getting different values for the cookies in different pages. Any ideas?
Thank you.
Cookies apply to the entire domain name. The cookies created with the code you posted will be available to any page hosted on your domain name.