Artpixler
Artpixler

Reputation: 485

jQuery: Set cookie for two domains

I want to set a cookie to a domain, but it should be available for a sub domain as well.

e.g. www.mydomain.com and sub.mydomain.com

When I set the cookie to the main domain it doesn't exist for the subdomain.

I use jQuery cookie Plugin: https://code.google.com/p/cookies/wiki/Documentation

My idea was to store it for both domains, have a look at the code:

var newOptions = {
    domain: 'sub.mydomain.com',
    secure: true
}
jaaulde.utils.cookies.set('name', 'value');
jaaulde.utils.cookies.set('name', 'value', newOptions );

What do I wrong?

Upvotes: 5

Views: 12737

Answers (2)

Jon
Jon

Reputation: 1234

Cash2m is correct, you should be able to specify a . to reach your subdomains:

$.cookie('key', 'value', { domain: '.mydomain.com' });

Upvotes: 5

Laurent S.
Laurent S.

Reputation: 6946

Well, if you want to extend it to the domain and not only a specific subdomain, just use

domain : 'mydomain.com'

instead of :

domain: 'sub.mydomain.com'

Upvotes: 0

Related Questions