javascript - Are cookies stored using document.cookie specific to a document? - Stack Overflow

I'm handling cookies using JavaScript to store some values in my asp web application.I use docume

I'm handling cookies using JavaScript to store some values in my asp 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.

I'm handling cookies using JavaScript to store some values in my asp 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.

Share Improve this question edited Aug 17, 2010 at 12:41 NLV asked Aug 17, 2010 at 12:30 NLVNLV 21.7k41 gold badges120 silver badges186 bronze badges 2
  • I'm not seeing anything wrong here. Have you tried creating a short-cut to javascript:alert(document.cookie) to inspect it at different points, and setting breakpoints in firebug or similar to make sure you aren't writing it before you're setting it? – Jon Hanna Commented Aug 17, 2010 at 12:46
  • Yes, i'm doing exactly what you have said. – NLV Commented Aug 17, 2010 at 12:53
Add a ment  | 

3 Answers 3

Reset to default 3

Cookies have a domain and a path. By default the domain will be the domain it is set from and the path will be the root path, but these can be over-ridden as follows:

Resource at http://www.example/foo/bar/baz sets a cookie (whether from the server or client-side javascript).

By default its domain is www.example and its path is / so it will be visible to all resources whose URI matches ://www.example/ where * is a simple wildcard.

Its domain can be set to example, but cannot be set to example - it can only be set to a domain that it is a subdomain of. (There are special, and imperfect, rules to stop you setting a cookie for a tld like )

Its path can be set to /foo/bar/baz or /foo/bar or even /foo/ba as it is pared with simple substring matching. If set to e.g. /foo/bar/ then it will be visible to a resource at http://www.example/foo/bar/qux but not one at http://www.example/foo/quux/corge

There is also the secure proprty, which restrict the cookie to the HTTPS protocol.


Edit: See http://www.quirksmode/js/cookies.html for details on how to actually set these properties.

The issue can be resolved with the help of below syntax

use ";path=/;" at the last while saving the cookies as shown below

document.cookie = c_name + "=" + oldInfo + ";path=/";

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744667644a4586847.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信