---
title: Set Cookies across all subdomains using JavaScript
description: >-
  Browser Cookies, is a very handy feature that enables us as web developers to do so much
  interactive programming. Recently since HTML5 rose up, its Web...
url: https://expiredqueues.com/subdomain-cookie-javascript/
date: '2013-07-15T08:02:31.000Z'
image: https://expiredqueues.com/images/og/subdomain-cookie-javascript-0272348d.jpg
---

Written in July 2013. Much of what it describes has since changed or disappeared; it is kept as a record of how the web used to work, not as advice.

Tutorials posts javascript

# How to set Cookies to share across all subdomains using JavaScript

15 July 2013 By Adrian 2 min read

Browser Cookies, is a very handy feature that enables us as web developers to do so much interactive programming. Recently since HTML5 rose up, its Web Storage is replacing this feature. But there is one area where Web Storage fails to achieve the result - subdomain access. There we have to again jump back to the old school document.cookie method.

In this very quick tutorial, let's discuss how we can set up browser cookies that can be accessed from all subdomains and root domain. But proceeding further requires basic knowledge of cookies, and if you need to learn, Tutorial Point has a good article on [JavaScript Cookies](http://www.tutorialspoint.com/javascript/javascript_cookies.htm).

## Steps:

1. Prepare the cookie name/value
2. Get the top level domain and assign as .domain.com
3. Set the path to root path=/
4. Set Expires (optional)

Let's set up an example cookie. Example taken as 'Last time report generated' to show up across subdomains.

```js

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

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

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

Major thing to take care here is:

Your domain must be in the format of **".domain.com"** - dot and root domain and your **path=/** always. If you don't setup you&#x72;**&#x20;path=/**, auto path will be saved as from where the cookie is being saved hence it won't be accessible across any subdomain.

You can try copy pasting the code above in the Console, and see the result in Resource Panel. If you happen to successfully run the script above, you will get a preview:

[![JavaScript Cookie](https://expiredqueues.com/images/opt/cookie-320.jpeg)](https://expiredqueues.com/images/2013/cookie.png)

Now that 'Report' cookie should be available across all subdomains like = main.cssjunction.com, tuts.cssjunction.com etc.

### You might be interested in:

[Flutter vs. React Native: What You Need to Know](https://expiredqueues.com/flutter-vs-react-native-what-you-need-to-know/)

[Previously Metro UI Form using HTML5 and CSS3](https://expiredqueues.com/html5-css3-metro-ui-form/) [Next All Code hosting on Github](https://expiredqueues.com/github-hosting/)

## Structured data

```json
{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "headline": "How to set Cookies to share across all subdomains using JavaScript",
  "datePublished": "2013-07-15T08:02:31.000Z",
  "dateModified": "2013-07-15T08:02:31.000Z",
  "author": {
    "@type": "Person",
    "name": "Adrian",
    "url": "https://expiredqueues.com/about/"
  },
  "publisher": {
    "@type": "Person",
    "name": "Adrian"
  },
  "mainEntityOfPage": "https://expiredqueues.com/subdomain-cookie-javascript/",
  "image": "https://expiredqueues.com/images/2013/cookie.png",
  "keywords": "posts, javascript, tips-tricks",
  "articleSection": "Tutorials"
}
```
