Firefox本地存储在其他选项卡中不可用

时间:2013-05-23 18:41:21

标签: javascript firefox

我正在创建一个firefox扩展。我想将localStorage用作整个浏览器的全局变量。但它仅适用于保存它的选项卡。我无法在其他标签中读取此值。我如何才能从任何标签访问它,或者可能出现什么问题?

我用它像:

localStorage.getItem('variable')
localStorage.setItem('variable','value')

更确切地说,我在加载时将javascript代码注入页面。从注入的代码我想将我的值保存到localstorage。

标签有不同的网址。并且我的代码在页面加载时尝试使用localstorage。但它检查localStorage值是否存在如下:

if(localStorage.getItem('variable')){ ... }

1 个答案:

答案 0 :(得分:-2)

我认为您不能将localStorage用作全局变量

localStorage is also the same as globalStorage[location.hostname], with the exception 
of being scoped to an HTML5 origin (scheme + hostname + non-standard port) and 
localStorage being an instance of Storage as opposed to 
globalStorage[location.hostname] being an instance of StorageObsolete which is covered 
below. For example, http://example.com is not able to access the same localStorage 
object as https://example.com but they can access the same globalStorage item. 
localStorage is a standard interface while globalStorage is non-standard so you 
shouldn't rely on these.

Please note that setting a property on globalStorage[location.hostname] does not set 
it on localStorage and extending Storage.prototype does not affect globalStorage 
items, only extending StorageObsolete.prototype does.

考虑使用globalStorage,然后在需要时设置localStorage

来源:https://developer.mozilla.org/en-US/docs/Web/Guide/DOM/Storage#localStorage