136

Below is a JavaScript cookie that is written on the user's computer for 12 months.

After we set the cookie on our main domain such as example.com, should the user visit a subdomain like test.example.com, we need to continue to identify the activity of the user across our "test" subdomain.

But with the current code, as soon as they leave www.example.com and visit test.example.com, they are no longer flagged as "HelloWorld".

Would anyone be able to help with my code to allow the cookie to be read across subdomains?

<script type="text/javascript"> var cookieName = 'HelloWorld'; var cookieValue = 'HelloWorld'; var myDate = new Date(); myDate.setMonth(myDate.getMonth() + 12); document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate; </script> 

4 Answers 4

258

Just set the domain and path attributes on your cookie, like:

<script type="text/javascript"> var cookieName = 'HelloWorld'; var cookieValue = 'HelloWorld'; var myDate = new Date(); myDate.setMonth(myDate.getMonth() + 12); document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate + ";domain=.example.com;path=/"; </script> 
Sign up to request clarification or add additional context in comments.

6 Comments

I'm trying to do this in localhost and I cannot change path
@Enve - Browsers treat localhost cookies a bit differently than other cookies. Or rather, they treat all cookies in a way that makes working with localhost difficult. For instance, see stackoverflow.com/questions/1134290/…. I suggest editing your hosts file and aliasing something like myserver.local to 127.0.0.1. Then you can use that to access your local server (and when setting cookies), and hopefully everything should work.
All cookie values you create & retrieve must be string values. Strings can contain characters that can upset the local storage when trying to retrieve them. One thing I would suggest is using the global encodeURI() & decodeURI() methods for the cookie name & value to handle any conversion that would need to take place. i.e. document.cookie = encodeURI(cookieName) +"=" + encodeURI(cookieValue).
If your server-side code is written in C#, Rick Strahl provides a method for getting the base domain, e.g. example.com, from the domain at weblog.west-wind.com/posts/2012/Apr/24/…
This doesn't seem to work for me. If I do this, followed by console.log(document.cookes); gives me a null return. Also nothing showing in browser storage.
|
45

You want:

document.cookie = cookieName +"=" + cookieValue + ";domain=.example.com;path=/;expires=" + myDate; 

As per the RFC 2109, to have a cookie available to all subdomains, you must put a . in front of your domain.

Setting the path=/ will have the cookie be available within the entire specified domain(aka .example.com).

2 Comments

FWIW - I think you need to remove the "path=expires=" piece and set it to "expires=".
As per the newer RFC 6265 it's no longer necessary to include the . in front of the domain.
10

Here is a working example :

document.cookie = "testCookie=cookieval; domain=." + location.hostname.split('.').reverse()[1] + "." + location.hostname.split('.').reverse()[0] + "; path=/" 

This is a generic solution that takes the root domain from the location object and sets the cookie. The reversing is because you don't know how many subdomains you have if any.

1 Comment

This will not work if there is second level hierarchy of domain extension e.g. .co.in
2

For Browser Extensions, You can also use the Cookies API and do:

browser.cookies.set({ url: 'example.com', name: 'HelloWorld', value: 'HelloWorld', expirationDate: myDate } 

MDN Set() Method Documentation

1 Comment

This solution only applies to browser extensions, not to javascript running in the context of a page.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.