【问题标题】:localStorage Getting NULL?localStorage 获取 NULL?
【发布时间】:2011-03-19 06:04:56
【问题描述】:

我不知道为什么,因为我以前做过这个并且效果很好,我认为这可能是因为浏览器问题/错误:

localStorage.setItem('foo', 'bar')
alert(localStorage.getItem('foo'));

我在 Firefox 3.6.6 中,它会提醒“栏”,但如果我这样做:

//localStorage.setItem('foo', 'bar')
alert(localStorage.getItem('foo'));

我得到 NULL。它应该仍然返回 bar,因为它在我的存储中。

另外,这只是一个运行它的网页。

【问题讨论】:

  • 抱歉,更新了我的问题,但基本上,它输出 null,但它仍应返回 bar,因为无论我离开页面、关闭浏览器或清除缓存,它都在我的存储中/历史/等等。
  • 您从哪里运行此代码?文件系统网页、本地服务器网页、远程服务器网页,还是来自 Firefox 插件?
  • @Oscar:你从哪里运行网页?如果它来自文件系统 (file://),Firefox 可能永远不会在 localStorage 中存储任何内容。
  • 大概就是这样。该死的,他们为什么要这样做?我想我在这里没有看到安全问题。
  • @Oscar – 我认为问题在于从本地文件系统运行的不同应用程序可以访问相同的变量,因为没有域可以区分它们。

标签: javascript html local-storage


【解决方案1】:

你是通过file:在本地运行脚本吗?

如果是这样,当使用 file: 访问权限时,Firefox 似乎不允许 localStorage 条目在卸载后继续存在。

有关更多信息,您可能需要查看以下问题:Is “localStorage” in Firefox only working when the page is online? 这有点过时了,但似乎仍然适用。

【讨论】:

  • 是的,我想就是这样......我在本地运行它尝试上传它,但读到这似乎就是这样
猜你喜欢
  • 1970-01-01
  • 2021-08-04
  • 2021-10-25
  • 1970-01-01
  • 2012-03-30
  • 2019-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多