如何在使用target =" _blank" /window.open()打开新窗口/选项卡时阻止sessionStorage被继承? [英] How to prevent sessionStorage being inherited when using target="_blank"/window.open() to open a new window/tab?

查看:509
本文介绍了如何在使用target =" _blank" /window.open()打开新窗口/选项卡时阻止sessionStorage被继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有网址 http://foo.com/ 的标签页上,我设置了一个sessionStorage项目 -

On a tab with url http://foo.com/ I set a sessionStorage item thus-

sessionStorage.bar="hello";

然后我在同一个域的任何路径上打开一个新窗口 -

I then open a new window on any path on the same domain -

window.open("http://foo.com/any/path");

然后在新窗口中我发现 -

Then on the new window I find that -

sessionStorage.bar === "hello"

是真的。如果我使用带有target =_ blank属性的链接打开新窗口,则会发生完全相同的事情。打开新选项卡时也会发生完全相同的事情,而不是新窗口。另外需要注意的是,这只适用于在打开新窗口之前在sessionStorage上设置的项目。在打开新窗口后,在任一窗口中添加或更改sessionStorage上的任何项目都不会以任何方式影响其他窗口。

Is true. The exact same thing happens if I use a link with target="_blank" attribute to open the new window. The exact same thing also happens when a new tab is opened, and not a new window. Another thing to note is that this is only true for items set on sessionStorage before opening the new window. Adding or changing any item on sessionStorage in either windows after the new window is opened does not effect the other window in any way.

我认为sessionStorage应该是作用域的到一个标签/窗口,但显然sessionStorage从另一个窗口打开时扩展到新的标签和窗口。

I thought that sessionStorage was supposed to be scoped to a single tab/window, but apparently sessionStorage is extended to new tabs and windows when they are opened from another window.

有没有办法防止这种情况发生?我可以测试window.opener的存在来检测这样的情况,但是如果可以首先防止它那么会更清洁。

Is there a way to prevent this? I can probably test for existence of window.opener to detect such a case, but it would be much cleaner if it was possible to prevent it in the first place.

谢谢!

推荐答案

根据 Webstorage规范,当在具有顶部的浏览上下文中创建新文档时在级别浏览上下文中,用户代理必须检查该顶级浏览上下文是否具有该文档原点的会话存储区域。如果是,那么这就是文档指定的会话存储区域。

According to the Webstorage Specification, "When a new Document is created in a browsing context which has a top-level browsing context, the user agent must check to see if that top-level browsing context has a session storage area for that document's origin. If it does, then that is the Document's assigned session storage area."

所以,我对此的看法是,如果你关闭标签,然后打开一个新选项卡,它将按照规范进行新的会话。但是,如果选项卡保持打开状态,然后您打开一个新选项卡,则顶级浏览上下文匹配,因此会引用sessionStorage。

So, my take on this is that if you close the tab, then open a new tab, it will be a new "session" per the specification. However, if the tab remains open and you then open a new tab, the top-level browsing context matches, so the sessionStorage is referenced.

这篇关于如何在使用target =" _blank" /window.open()打开新窗口/选项卡时阻止sessionStorage被继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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