localStorage.setItem不会持久刷新 [英] localStorage.setItem not persisting on refresh

查看:646
本文介绍了localStorage.setItem不会持久刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用HTML5本地存储来执行一个简单的setItem和getItem。它似乎没有工作。这有效:

I'm trying to do a mega-simple setItem and getItem using HTML5 local storage. It just doesn't seem to work though. This works:

$(document).ready(function () {
  localStorage.setItem('keyA', 'valueA');
  var testA = localStorage.getItem('keyA');
  alert(testA);
});

它输出一个警告框,上面写着'valueA'。但是,当我注释第2行(设置项目值)并刷新页面时,它只会提示空。

It outputs an alert box saying 'valueA'. But when I comment out line 2 (which sets the item value) and refresh the page it just alerts 'null'.

为什么值不会持续存在?它就像它实际上根本没有存储一样。

Why is the value not persisting? It's like it's just not actually getting stored at all.

浏览器是Firefox 6,所以没问题。可以在jquery document.ready中调用它吗?我用谷歌搜索了但没看到任何相关内容。

The browser is Firefox 6, so no problem there. Could it be something to do with calling it in the jquery document.ready? I've googled but couldn't see anything about that.

如果有人能让我超过这个最初的障碍我会非常感激,谢谢!

If anyone could get me over this initial hurdle I'd be most grateful, thanks!

推荐答案

好的,经过很多挫折我得到了解决方案。基本上,我只是从文件系统本地运行它作为快速概念证明。它在Firefox和IE9中都不起作用,但它确实可以在Chrome中运行。

Okay, after a lot of frustration I have the solution. Basically, I was running this locally just from the filesystem as a 'quick' proof of concept. It didn't work in Firefox nor in IE9 but it did work in Chrome.

我最终做的是在一个真实的域上尝试这个,而这似乎有完成这个技巧。

What I ended up doing was trying this on a real domain, and that seems to have done the trick.

所以我可以得出的结论是Firefox中的localStorage(至少6.0.2)和IE9在文件系统路径上运行时不起作用。它在Chrome中。 Firefox和IE9需要一个适当的域来运行,大概是因为它们将localStorate对象与域相关联的方式比Chrome更严格(在Chrome中它不需要像域一样)。

So the conclusion I can draw is that localStorage in Firefox (6.0.2 at least) and IE9 does not work when run on a file-system path. It does in Chrome. Firefox and IE9 require a 'proper' domain to run from, presumably because they are more strict than Chrome in the way they associate the localStorate object to a 'domain' (in Chrome it doesn't need to be a domain as such).

我希望这对人们有所帮助,因为它让我感到沮丧! :)

I hope this has helped people as it's frustrated the hell out of me! :)

这篇关于localStorage.setItem不会持久刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆