admin管理员组文章数量:1134247
Below is a JavaScript cookie that is written on the user's computer for 12 months.
After we set the cookie on our main domain such as example
, should the user visit a subdomain like test.example
, we need to continue to identify the activity of the user across our "test" subdomain.
But with the current code, as soon as they leave www.example
and visit test.example
, they are no longer flagged as "HelloWorld".
Would anyone be able to help with my code to allow the cookie to be read across subdomains?
<script type="text/javascript">
var cookieName = 'HelloWorld';
var cookieValue = 'HelloWorld';
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 12);
document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate;
</script>
Below is a JavaScript cookie that is written on the user's computer for 12 months.
After we set the cookie on our main domain such as example.com
, should the user visit a subdomain like test.example.com
, we need to continue to identify the activity of the user across our "test" subdomain.
But with the current code, as soon as they leave www.example.com
and visit test.example.com
, they are no longer flagged as "HelloWorld".
Would anyone be able to help with my code to allow the cookie to be read across subdomains?
<script type="text/javascript">
var cookieName = 'HelloWorld';
var cookieValue = 'HelloWorld';
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 12);
document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate;
</script>
Share
Improve this question
edited Jul 23, 2017 at 16:56
Brett DeWoody
62.7k31 gold badges144 silver badges191 bronze badges
asked Apr 15, 2011 at 1:14
EvanEvan
3,4817 gold badges39 silver badges53 bronze badges
4 Answers
Reset to default 254Just set the domain
and path
attributes on your cookie, like:
<script type="text/javascript">
var cookieName = 'HelloWorld';
var cookieValue = 'HelloWorld';
var myDate = new Date();
myDate.setMonth(myDate.getMonth() + 12);
document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate
+ ";domain=.example.com;path=/";
</script>
You want:
document.cookie = cookieName +"=" + cookieValue + ";domain=.example.com;path=/;expires=" + myDate;
As per the RFC 2109, to have a cookie available to all subdomains, you must put a .
in front of your domain.
Setting the path=/ will have the cookie be available within the entire specified domain(aka .example.com
).
Here is a working example :
document.cookie = "testCookie=cookieval; domain=." +
location.hostname.split('.').reverse()[1] + "." +
location.hostname.split('.').reverse()[0] + "; path=/"
This is a generic solution that takes the root domain from the location object and sets the cookie. The reversing is because you don't know how many subdomains you have if any.
For Browser Extensions, You can also use the Cookies API and do:
browser.cookies.set({
url: 'example.com',
name: 'HelloWorld',
value: 'HelloWorld',
expirationDate: myDate
}
MDN Set()
Method Documentation
本文标签: Creating a JavaScript cookie on a domain and reading it across sub domainsStack Overflow
版权声明:本文标题:Creating a JavaScript cookie on a domain and reading it across sub domains - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736815574a1954075.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论