最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

http - Javascript set cookie for current domain including subdomains - Stack Overflow

programmeradmin2浏览0评论

Is it possible to store a JS cookie for the current domain including subdomains.

e.g.: document.cookie = "key=value; expires=Tue, 16 Apr 2019 11:31:56 GMT; path=/;secure" sets a cookie for the current domain but does not add a dot to the domain name.

I know that it is possible to specify the domain via domain=.example but I do not want to hardcode the domain name.

I tried something like this but it did not work out: document.cookie = "key=value; expires=Tue, 16 Apr 2019 11:31:56 GMT; path=/;secure;domain=."

Update:

I know you can get the current domain with window.location.hostname but is there a solution where i do not need to get the domain name programmatically

UPDATE 2:

like described here: What does the dot prefix in the cookie domain mean?

The leading dot means that the cookie is valid for subdomains as well; nevertheless recent HTTP specifications (RFC 6265) changed this rule so modern browsers should not care about the leading dot. The dot may be needed by old browser implementing the deprecated RFC 2109.

This means that it does not make a difference if you use a dot before the domain name in modern browsers. That said, you can leave the domain section of the JS cookie blank and it is set to the current domain (which also matches subdomains)

Is it possible to store a JS cookie for the current domain including subdomains.

e.g.: document.cookie = "key=value; expires=Tue, 16 Apr 2019 11:31:56 GMT; path=/;secure" sets a cookie for the current domain but does not add a dot to the domain name.

I know that it is possible to specify the domain via domain=.example. but I do not want to hardcode the domain name.

I tried something like this but it did not work out: document.cookie = "key=value; expires=Tue, 16 Apr 2019 11:31:56 GMT; path=/;secure;domain=."

Update:

I know you can get the current domain with window.location.hostname but is there a solution where i do not need to get the domain name programmatically

UPDATE 2:

like described here: What does the dot prefix in the cookie domain mean?

The leading dot means that the cookie is valid for subdomains as well; nevertheless recent HTTP specifications (RFC 6265) changed this rule so modern browsers should not care about the leading dot. The dot may be needed by old browser implementing the deprecated RFC 2109.

This means that it does not make a difference if you use a dot before the domain name in modern browsers. That said, you can leave the domain section of the JS cookie blank and it is set to the current domain (which also matches subdomains)

Share Improve this question edited Apr 25, 2018 at 6:40 warch asked Apr 20, 2018 at 6:26 warchwarch 2,6092 gold badges29 silver badges47 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

Possible solution:

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

评论列表(0)

  1. 暂无评论