admin管理员组文章数量:1353228
I set a cookie:
<script>
document.cookie="cid1={{utm_campaign}}; path=/;"
</script>
The {{}} is a macro and is Google-Tag-Manager syntax, please ignore that.
The script above is triggered whenever anyone lands on example with a tag like so: example/?utm_medium=test&utm_source=bla&utm_campaign=foo. I tested it and sure enough, when I land on the home page with these parameters the cookie is set.
But a visitor can move to a subdomain dogs.example. When I looked in the console the cookie cid1 is no longer there.
Is there a setting I can change when creating the cookie other than setting the path to "/" so that the cookie crosses to the subdomain?
I set a cookie:
<script>
document.cookie="cid1={{utm_campaign}}; path=/;"
</script>
The {{}} is a macro and is Google-Tag-Manager syntax, please ignore that.
The script above is triggered whenever anyone lands on example. with a tag like so: example./?utm_medium=test&utm_source=bla&utm_campaign=foo. I tested it and sure enough, when I land on the home page with these parameters the cookie is set.
But a visitor can move to a subdomain dogs.example.. When I looked in the console the cookie cid1 is no longer there.
Is there a setting I can change when creating the cookie other than setting the path to "/" so that the cookie crosses to the subdomain?
Share Improve this question asked Apr 23, 2015 at 12:55 Doug FirDoug Fir 21.4k54 gold badges191 silver badges340 bronze badges2 Answers
Reset to default 6Domain should be something like .example. so *.example. can access it
var website_host = window.location.hostname.replace('www.', '');
document.cookie = "cid1={{utm_campaign}}; path=/;domain=."+website_host
// to be something like this"cid1={{utm_campaign}}; path=/;domain=.example."
You're missing the domain-parameter for this. Set domain to .example.
to make it accessable from all pages in .example.
.
<script>
document.cookie="cid1={{utm_campaign}}; path=/; domain=.example."
</script>
Duplicate here: setting cross-subdomain cookie with javascript
;domain=domain (e.g., 'example.', '.example.' (includes all subdomains), 'subdomain.example.') If not specified, defaults to the host portion of the current document location.
Full documentation: https://developer.mozilla/en-US/docs/Web/API/Document/cookie
本文标签: javascriptmake cookie persist across subdomainStack Overflow
版权声明:本文标题:javascript - make cookie persist across subdomain - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743928282a2563368.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论