Javascript sessionStorage 跨选项卡? [英] Javascript sessionStorage Across tabs?

查看:69
本文介绍了Javascript sessionStorage 跨选项卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 sessionStorage 来确保每个会话只发生一次操作.但是,如果用户打开一个新窗口 - sessionStorage 不会转移到新窗口,并且该操作将再次执行.请参阅下面的代码:

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

是否有一种适用于所有标签的简单方法?也许我需要使用 cookie?

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

推荐答案

就 html5 存储而言,我会选择 localStorage.

As far as html5 storage goes i would go for localStorage in your case.

有关差异,请参见下文.

See below for the differences.

会话存储:

  • 值仅在它们所在的窗口或选项卡中持续存在
    存储.
  • 值仅在窗口或选项卡中可见创造了它们.

本地存储:

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

在此处阅读更多信息

这篇关于Javascript sessionStorage 跨选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆