Creating a JavaScript Cookie on a Domain and Reading It Across Sub Domains

Creating a JavaScript cookie on a domain and reading it across sub domains

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>

Javascript set cookie for current domain including subdomains

Possible solution:

var domainName = window.location.hostname;
document.cookie = "key=value; expires=Tue, 16 Apr 2019 11:31:56 GMT; path=/; secure; domain=." + domainName;

setting cross-subdomain cookie with javascript

Here is a link on how to share cookies amongst a domain:

https://www.thoughtco.com/javascript-by-example-2037272

It involves setting the domain attribute of the cookie string like:

document.cookie = "myValue=5;path=/;domain=example.com";

This cookie should now be accessible to all sub domains of example.com like login.example.com

Share cookies between subdomain and domain

If you set a cookie like this:

Set-Cookie: name=value

then the cookie will only apply to the request domain, and will only be sent for requests to the exact same domain, not any other subdomains. (See What is a host only cookie?)

Two different domains (e.g. example.com and subdomain.example.com, or sub1.example.com and sub2.example.com) can only share cookies if the domain attribute is present in the header:

Set-Cookie: name=value; domain=example.com

The domain attribute must "domain-match" the request URL for it to be valid, which basically means it must be the request domain or a super-domain. So this applies for both examples in the question, as well as sharing between two separate subdomains.

This cookie would then be sent for any subdomain of example.com, including nested subdomains like subsub.subdomain.example.com. (Bear in mind there are other attributes that could restrict the scope of the cookie and when it gets sent by the browser, like path or Secure).

Because of the way the domain-matching works, if you want sub1.example.com and sub2.example.com to share cookies, then you'll also share them with sub3.example.com.

See also:

  • www vs no-www and cookies
  • setcookie.net: a site where you can try it out (disclaimer: developed by me, for this question)

A note on leading dots in domain attributes: In the early RFC 2109, only domains with a leading dot (domain=.example.com) could be used across subdomains. But this could not be shared with the top-level domain, so what you ask was not possible in the older spec.

However, the newer specification RFC 6265 ignores any leading dot, meaning you can use the cookie on subdomains as well as the top-level domain.

Creating a JavaScript cookie on a domain and reading it across sub domains

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>

Will the cookies be shared across multiple subdomains, if all subdomains point to the same single website?

I suggest you read this question post, in summary it says that what's important is the domain parameter you define to set the cookie. What your looking for is something like this ".mydomain.com".

JavaScript cookies not working on sub-domains

When setting up the cookie, your domain must be in format of .domain.com – dot and root domain and path=/, always.

If you don't set path=/, auto path will be saved as from where the cookies is being saved - hence it wont be accessible across any subdomain.

//variables
var LastReportGenerated="Jul 11 2013",
baseDomain = '.cssjunction.com',
expireAfter = new Date();

//setting up cookie expire date after a week
expireAfter.setDate(expireAfter.getDate() + 7);

//now setup cookie
document.cookie="Report={'ReportName':'MainReport', 'lastGenerated':" + LastReportGenerated + "}; domain=" + baseDomain + "; expires=" + expireAfter + "; path=/";

Source:

How to set cookies to share across all subdomains using JavaScript



Related Topics



Leave a reply



Submit