views:

147

answers:

3
+1  Q: 

Javascript Cookie

I have cookies for the domain .forum.mywebsite.com and for .mywebsite.com.

Is it possible to read the cookie for the .mywebsite.com domain with javascript from the forum.mywebsite.com location?

A: 

The cookie is sent to the server as part of the request, it's not something that's accessible from the client as far as I'm aware (therefore not accessible to javascript).

Lazarus
+1  A: 

Yes you should be able to read it. Check the following articles

When the JavaScript set cookies process is invoked, the script either presents the browser with a domain, or a blank value. If no domain is given it is assumed to be the domain of the page i.e. java-programming.suite101.com in this case.

The JavaScript cookies path, on the other hand, allows the programmer to make sure that the cookie is only valid (sent to the server) for pages in a specific path on the website. So, specifying a path such as /blog would restrict the cookie to my.domain.com/blog. If the cookie should be applicable across the whole (sub)domain, then path=/ should be specified.

  1. Javascript Cookies
  2. Using Javascript Cookies

But you will not be able to delete the cookie from the subdomain. Check this question in SO : Is it possible to delete subdomain cookies?

Shoban
A: 

You can set the domain which has access to a given cookie via adding the cookie attribute value

domain={domainname};

If {domainname} starts with a leading ., any subdomain may access the cookie as well (source: MDC).

According to RFC 2965, which is referenced by the W3C spec, the leading . shouldn't be necessary, but better safe than sorry ;)

Christoph
but i'm on the subdomain and i'd like to access the top domain cookie.i've tried document.cookie, but the top-domain cookies aren't there.
János Harsányi