开发者

Are cookies stored using document.cookie specific to a document?

开发者 https://www.devze.com 2023-01-11 18:01 出处:网络
I\'m handling cookies using JavaScript to store some values in my asp.net web application. I use document.cookie to 开发者_StackOverflow中文版save some values (converted into a lengthy string). But i

I'm handling cookies using JavaScript to store some values in my asp.net web application. I use document.cookie to 开发者_StackOverflow中文版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 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.net/foo/bar/baz sets a cookie (whether from the server or client-side javascript).

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

Its domain can be set to example.net, but cannot be set to example.org - 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 .net)

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

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


Edit: See http://www.quirksmode.org/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.

0

精彩评论

暂无评论...
验证码 换一张
取 消