为所有浏览器标签设置Cookie [英] Setting Cookie for all browser tabs

查看:155
本文介绍了为所有浏览器标签设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JavaScript设置cookie,该cookie可用于浏览器中的所有选项卡.

I want to set a cookie using javascript, which would be available for all the tabs in browser.

我用过document.cookie = "tracker=ABC"

即使我也尝试设置路径,但结果仍然相同 document.cookie = "tracker=ABC;path=/"

Even I have tried to set the path also, but result remained same document.cookie = "tracker=ABC;path=/"

但是,这似乎仅适用于设置它的选项卡.此cookie对浏览器中的其他选项卡不可见..

However, this seems applicable only to the tab where it was set. This cookie is not visible to other tabs in the browser..

此外,对于cookie的路径概念,我也不是很清楚.如果有人能告诉我有关它作为次要答案的信息,那将很好

Also, I am not very clear with the path concept in cookies. Would be great if someone can tell me about it's use as a secondary answer

此致

SAP学习者

所以我的问题是:如何设置适用于浏览器中所有选项卡的cookie

So my question is: How can I set a cookie which would be applicable for all the tabs in the browser

推荐答案

cookie不会链接到浏览器选项卡,而是链接到域及其子域.

A cookie is not linked to a browser tab, but to a domain and its sub-domains.

如果您无法创建交叉表" cookie,则可能是因为域不同.这是所有浏览器都实施的一项明显的安全规则,该规则可阻止域访问其他域的cookie.

If you couldn't create a "cross-tab" cookie, it's likely because the domains are different. It's an obvious security rule implemented by all browsers, that prevents domains from accessing other domains cookies.

如果域相同,则可以使用,您可以自己验证:

If the domains are the same, then it works and you can verify it by yourself:

  • 打开一个标签,然后打开一些网站
  • 在控制台中使用document.cookie = "test"创建一些cookie
  • 打开另一个标签,然后打开相同的网站
  • 在控制台中
  • 键入document.cookie,您将能够看到test cookie
  • open a tab, then open some website
  • create some cookie with document.cookie = "test" in the console
  • open another tab, then open the same website
  • type document.cookie in the console and you'll be able to see the test cookie

这篇关于为所有浏览器标签设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆