localStorage不保存数据
我正在开发 Firefox 扩展并将本地数据保存在 localStorage 中。 所以我在本地保存的网页上测试我的扩展。
window.content.localStorage.setItem('name', 'Hello World!');
并且 window.content.localStorage.getItem('name')
返回正确的字符串。 但是当我重新打开此测试页面时 window.content.localStorage.getItem('name')
返回“null”。 为什么?是因为我使用的是本地页面吗?
I'm developing firefox extension and going to save local data in localStorage.
So I testing my extension on local saved web page.
window.content.localStorage.setItem('name', 'Hello World!');
And window.content.localStorage.getItem('name')
return correct string.
But when I reopened this testing page window.content.localStorage.getItem('name')
returns 'null'.
Why? It's because I'm using local page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当前,不同的浏览器对 localStorage 的 file: url 处理方式不同。
这意味着,如果您只是在计算机上创建文件并直接打开它,根据您使用的浏览器,您将得到不同的结果。
我相信这在 Firefox 8 中有所改变,所以如果您使用 8 或更高版本,它应该适合您。其他浏览器我不确定。
编辑:“localStorage”仅在 Firefox 中工作当页面在线时? 似乎确认 FF3-8 将无法按照您的方式工作。
Different browsers currently treat file: urls differently for localStorage.
This means that if you are just creating a file on your computer and opening it directly, you will have different results depending on the browser you are using.
I believe this was changed in Firefox 8, so if you use 8 or higher it should work for you. Other browsers I am not sure about.
Edit: Is "localStorage" in Firefox only working when the page is online? seems to confirm that FF3-8 will not work how you are doing it.