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

后端 未结 2 1917
长发绾君心
长发绾君心 2021-01-18 03:24

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 t

相关标签:
2条回答
  • 2021-01-18 03:31

    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.

    0 讨论(0)
  • 2021-01-18 03:35

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

    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.

    0 讨论(0)
提交回复
热议问题