浏览器会在选项卡之间传播javascript变量吗? [英] Do browsers propagate javascript variables across tabs?
问题描述
我有一些JavaScript可以查看是否设置了var,如果设置了var,请执行一些操作(如果没有设置)。
I have some JavaScript that will look to see if a var is set, if it is set then do something if not don't.
if (reset === 'reset') {
gallery.unformat(container);
}
重置
仅页面加载后设置。因此,该脚本仅在用户重新加载页面后执行。
reset
is only set once the page has loaded. So this script will only execute after the user reloads the page.
如果我在Firefox中打开新标签页,则未设置重置
。如果我在Chrome中打开新标签页,则会设置重置
。
If i open a new tab in firefox reset
isn't set. If i open a new tab in chrome reset
is set.
因此,对于我来说,chrome可以正确处理它,并且只有在第一次访问该站点时,此var才会被设置,因此一切正常。
So for my case chrome handles it correctly and only on the first going to the site does this var get set and thus everything works correctly.
我想知道变量是否跨制表符传播,如果是这样的话,哪些浏览器会做什么?
I want to know is do variables propagate across tabs and if so which browsers do what?
推荐答案
变量应该 不 在新标签中保留。我无法重现您在Chrome中看到的行为。
Variables should not persist across new tabs. I can't reproduce the behaviour you see in Chrome.
您应该使用 cookies
,或者如果您想成为HTML5,请查看 localStorage
。
You should looking at using cookies
, or if you want to be HTML5, look at localStorage
.
这两个都是特定于域的,而不是按选项卡的。
Both of these are domain specific, rather than per-tab.
例如在 localStorage
中,您可以这样做;
E.g. in localStorage
, you could go for;
// Check that localStorage is supported in the current browser, and then try
// to retrieve the item.
if ('localStorage' in window && localStorage.getItem('reset') === 'reset') {
gallery.unformat(container);
}
然后您就可以设置 reset
之后通过;
You'd then be able to set reset
later via;
localStorage.setItem('reset', 'reset');
这篇关于浏览器会在选项卡之间传播javascript变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!