JLF
JLF

Reputation: 2370

Set cookie for ONLY two different subdomains

I want to set a cookie that will be available to sub1.example.com and sub2.example.com but not sub3.example.com.

I know making the domain value .example.com will open the value to all subdomains, but I want it to be limited to just these two. Is there a way to do this?

Upvotes: 2

Views: 554

Answers (1)

Phillip Chan
Phillip Chan

Reputation: 983

I don't think there's a way to store data based on subdomain. A workaround idea could be to store the subdomain as part of the data and let your application run the behavior based on it.

1) Get the subdomain:

var parts = location.hostname.split('.');
var subdomain = parts.shift();

2) Then you could save that subdomain as part of the data you're saving

localStorage.setItem(subdomain, 'someData');

3) Then run a conditional when you need to operate only on a certain subdomain.

var parts = location.hostname.split('.');
var subdomain = parts.shift();

if (subdomain !== "sub3") {
    // run your functionality
    var data = storage.getItem(subdomain);
}

That way, your 1) storage is available on sub1 and sub2, 2) but the functionality doesn't run on sub3

Upvotes: 1

Related Questions