I have been working with cookies in my asp.net web application using JavaScript to store various values. I am currently using document.cookie
to save these values as a lengthy string. However, I am encountering an issue where the value I save in the cookie is only accessible within the current page URL.
For example, if I save a value in a cookie on http://myapp/doc1.aspx
, I am unable to retrieve it on http://myapp/doc2.aspx
.
Is the scope of document.cookie limited to a single page? How can I ensure that cookies can be saved and read across all pages on the site?
Update
Below is my current code for getting and setting 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;
}
However, I am experiencing issues with the values of the cookies being different on various pages. Any insights or suggestions would be greatly appreciated.
Thank you.