localStorage不保存数据

发布于 2024-12-25 02:19:38 字数 335 浏览 4 评论 0原文

我正在开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久而酒知 2025-01-01 02:19:38

当前,不同的浏览器对 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文