Javascript sessionStorage Across tabs?

mal*_*ers 4 javascript session-variables session-cookies

I am using sessionStorage to ensure an action only happens once per session. However, if a user opens a new window - the sessionStorage is not carried over to the new window, and the action is executed again. See my code below:

if sessionStorage.alreadyTracked != "1"
  sessionStorage.alreadyTracked = "1"
  ...
Run Code Online (Sandbox Code Playgroud)

Is there an easy way to do this that applies to all tabs? Maybe I need to use a cookie?

Wil*_*Gfx 5

就 html5 存储而言,我会在您的情况下使用 localStorage。

有关差异,请参见下文。

会话存储:

  • 值仅在它们
    存储的窗口或选项卡中持续存在。
  • 值仅在创建它们的窗口或选项卡中可见。

本地存储:

  • 值保持窗口和浏览器的生命周期。
  • 值在运行在同一来源的每个窗口或选项卡之间共享。

在这里阅读更多