简体   繁体   English

使用 target="_blank"/window.open() 打开新窗口/选项卡时如何防止 sessionStorage 被继承?

[英]How to prevent sessionStorage being inherited when using target="_blank"/window.open() to open a new window/tab?

On a tab with url http://foo.com/ I set a sessionStorage item thus-在带有 url http://foo.com/的选项卡上,我因此设置了一个 sessionStorage 项目-

sessionStorage.bar="hello";

I then open a new window on any path on the same domain -然后我在同一域上的任何路径上打开一个新的 window -

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

Then on the new window I find that -然后在新的 window 上,我发现-

sessionStorage.bar === "hello"

Is true.是真的。 The exact same thing happens if I use a link with target="_blank" attribute to open the new window.如果我使用带有 target="_blank" 属性的链接打开新的 window,则会发生完全相同的事情。 The exact same thing also happens when a new tab is opened, and not a new window.当打开一个新选项卡而不是新的 window 时,也会发生完全相同的事情。 Another thing to note is that this is only true for items set on sessionStorage before opening the new window.另外需要注意的是,这仅适用于在打开新的 window 之前在 sessionStorage 上设置的项目。 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.打开新的 window 后,在 windows 中的 sessionStorage 上添加或更改任何项目不会以任何方式影响其他 window。

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 从另一个 window 打开时扩展到新选项卡和 windows。

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!谢谢!

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。

I solved this by temporary removing the session item while opening the new window.我通过在打开新的 window 时临时删除 session 项目解决了这个问题。

sessionStorage.removeItem('bar');
window.open('http://example.com/any/path', '_blank');
sessionStorage.setItem('bar', 'hello');

You can using this way:您可以使用这种方式:

    const a = document.createElement('a');
    a.href = "http://example.com/any/path";
    a.target = '_blank';
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);

You can workaround the inheritance behavior by using a separate key per window (or if you have multiple keys, by applying some unique prefix to each key). 您可以通过在每个窗口使用单独的密钥来解决继承行为(或者如果您有多个密钥,则通过为每个密钥应用一些唯一的前缀)。

That begs the question of how to assign/retain a unique key per window. 这引出了如何为每个窗口分配/保留唯一键的问题。 Well, SessionStorage is not empty when link opened in new tab in Internet Explorer shows that you can set the name of each window to some unique value, which is retained across reloads. 好吧, 当Internet Explorer中的新选项卡中打开的链接显示您可以将每个窗口的名称设置为某个唯一值(在重新加载时保留) SessionStorage不为空

You can prevent a new tab or window from inheriting a copy of the original window's sessionStorage by setting the window.open windowFeatures parameter 'noopener' to true:您可以通过将 window.open windowFeatures 参数 'noopener' 设置为 true 来防止新选项卡或 window 继承原始窗口 sessionStorage 的副本:

window.open('http://example.com', undefined, 'noopener=true');

The parameter 'noreferrer' also works because, if set, the browser will omit the Referer header, as well as set noopener to true.参数 'noreferrer' 也有效,因为如果设置,浏览器将省略 Referer header,并将 noopener 设置为 true。

Mozilla documentation for window.openwindow.open 的 Mozilla 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM