jQuery: Set cookie for two domains

11,542

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

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

Related videos on Youtube

Artpixler
Author by

Artpixler

Updated on June 06, 2022

Comments

  • Artpixler
    Artpixler almost 2 years

    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?

  • HenningCash
    HenningCash almost 11 years
    I think you meant domain : '.mydomain.com'
  • Laurent S.
    Laurent S. almost 11 years
    No I didn't... It should work that way, the starting point has no sense.
  • Artpixler
    Artpixler almost 11 years
    Thanks for your answer. I did change the domain to mydomain.com but I still get undefined when I try to get the cookie