Creating a JavaScript cookie on a domain and reading in subdomains

综合技术 Hello, buddy! (源链)

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?

  var cookieName = 'HelloWorld';
  var cookieValue = 'HelloWorld';
  var myDate = new Date();
  myDate.setMonth(myDate.getMonth() + 12);
  document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate;

Just set the domain
and path
attributes on your cookie, like:

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=/";

您可能感兴趣的

Demystifying the concepts of ‘Asynchronous JavaScr... Javascript has been one of the vital constituents for application development nowadays in the world of information technology. Every application that ...
一种绕过限制下载论文的思路 * 本文作者:tammypi,本文属FreeBuf原创奖励计划,未经许可禁止转载 注:本文下面的内容仅讨论绕过思路,作为技术交流之用。大家下载论文还是应该通过正规渠道,付费下载,尊重各位站长的劳动成果。敏感图片和代码中涉及站点的内容均已打码。 有时候要研究技术,我们也需要下载一些论文来看看的...
解读 JavaScript 之引擎、运行时和堆栈调用... 随着 JavaScript 变得越来越流行,很多团队在他们的堆栈中实现诸多层级的支持 - 前端、后端、混合应用程序、嵌入式设备等等。 本文是该系列文章的第一篇,旨在深入研究 JavaScript 及其实际工作原理:我们认为通过了解 JavaScript 的构建块以及它们如何一起协作的,你将能够...
GetProperty Problem I have an ASP.net website and inside its .aspx page there is a javascript function and from my silverlight project , i want to get a value of proper...
Unable to get the id or value of the clicked item This is the Html code: 'fav_id_002' %> ...
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » Creating a JavaScript cookie on a domain and reading in subdomains



专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录