How to prevent sessionStorage being inherited when using target=“_blank”/window.open() to open a new window/tab?

时间:2022-08-26 07:52:27

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

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

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"

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.

是真的。如果我使用带有target =“_ blank”属性的链接打开新窗口,则会发生完全相同的事情。打开新选项卡时也会发生完全相同的事情,而不是新窗口。另外需要注意的是,这只适用于在打开新窗口之前在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.

我认为sessionStorage应该被限定为单个选项卡/窗口,但显然sessionStorage从另一个窗口打开时会扩展到新的选项卡和窗口。

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.

有办法防止这种情况吗?我可以测试window.opener是否存在以检测这种情况,但是如果可以首先防止它那么会更加清晰。

Thanks!

1 个解决方案

#1


5  

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."

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

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.

因此,我对此的看法是,如果您关闭选项卡,然后打开一个新选项卡,它将是每个规范的新“会话”。但是,如果选项卡保持打开状态,然后打开新选项卡,则*浏览上下文会匹配,因此会引用sessionStorage。

#1


5  

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."

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

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.

因此,我对此的看法是,如果您关闭选项卡,然后打开一个新选项卡,它将是每个规范的新“会话”。但是,如果选项卡保持打开状态,然后打开新选项卡,则*浏览上下文会匹配,因此会引用sessionStorage。