【问题标题】:sessionStorage on new window isn't empty, when following a link with target="_blank"关注带有 target="_blank" 的链接时,新窗口上的 sessionStorage 不为空
【发布时间】:2013-06-22 05:56:12
【问题描述】:

我在关注 Web 链接时看到了 HTML5 sessionStorage 的不同行为。有时第一页的 sessionStorage 被复制到 TargetPage,有时我在 TargetPage 上得到一个空的 sessionStorage。

谁能解释一下,为什么会这样?

给出以下链接:

<a href="TargetPage.html" target="_blank">link</a>

当我通过右键单击打开链接->“在新选项卡中打开链接”时,TargetPage 上的 sessionStorage 为空。 (预期) 但是当我通过正常的左键单击打开链接时,sessionStorage 会被复制到 TargetPage。 (意外)

注意:我在 Chrome 和 Firefox 中看到过这种行为。 Internet Explorer 9 在这两种情况下都提供了一个空的 sessionStorage。这是我的预期行为。

我创建了一个 jsFiddle 来演示该行为:http://jsfiddle.net/P9nUv/3/

谁能想到一个跨浏览器的解决方案来确保新的浏览器选项卡/窗口的 sessionStorage 为空,无论它们是如何打开的?

感谢任何帮助!

【问题讨论】:

    标签: html cross-browser session-storage


    【解决方案1】:

    恕我直言:

    我认为问题出在不同的browsing context 上。在W3CwebStorage规范中有一段:

    当在浏览上下文中创建一个新文档时,该文档具有 顶级浏览上下文,用户代理必须检查是否 顶级浏览上下文有一个会话存储区 文件的来源

    Document 在此引用中表示新页面。 所以这意味着如果你在新标签页中打开相同的页面,它使用相同的browsing context 和相同的 sessionStorage,但如果你在新窗口中打开(即 target="_blank")browsing context 是不同的。

    或

    问题可能出在单独的 CPU 线程上,因此将 browsing context 和 sessionStorages 分开。

    【讨论】:

    • 我认为可能是这种情况:When a new top-level browsing context is created by a script in an existing browsing context, or by the user following a link in an existing browsing context, or in some other way related to a specific Document, and the creation is not a new start for session storage, then the session storage area of the origin of that Document must be copied into the new browsing context when it is created. From that point on, however, the two session storage areas must be considered separate, not affecting each other in any way.
    【解决方案2】:

    又是一个古老的问题,但我在这个网站上没有找到任何答案,我在 Firefox 44 上也遇到了同样的问题。我找到了一个解决方法,你可以在这里找到它:https://stackoverflow.com/a/35596134/3751509

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-20
      • 2015-01-16
      • 1970-01-01
      • 2014-11-01
      • 2020-08-09
      • 2011-02-02
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多