HTML5 本地存储在页面刷新之间不保存
我是第一次使用 FF5 使用 HTML5 本地存储。我有下面的 javascript,它应该将字符串保存到本地存储中,但是当重新加载页面时,本地存储中没有任何值。我做错了什么?为什么加载后的页面上没有填充“foo”?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js</script>
<script>
$(document).ready(function() {
if (!localStorage) {return false; }
var foo = localStorage.getItem("key1");
if (foo != null)
{
document.getByName("identifier").value = foo;
}
localStorage["key1"] = "value1";
});
</script>
I am using HTML5 local storage for the first time using FF5. I have the below javascript, it should save a string into local storage, however when the page is reloaded there isn't any values in local storage. What am I doing wrong? Why is 'foo' not populated on the pages sedond load?
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js</script>
<script>
$(document).ready(function() {
if (!localStorage) {return false; }
var foo = localStorage.getItem("key1");
if (foo != null)
{
document.getByName("identifier").value = foo;
}
localStorage["key1"] = "value1";
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用
localStorage.setItem
。例如注意:IE 7 不支持 localStorage
You should use
localStorage.setItem
. For exampleNOTE: IE 7 does not have localStorage support
我也遇到过同样的问题。我发现在 document.ready 期间无法从本地存储中检索项目,但之后我可以获取这些项目。
I've also had this same issue. I've discovered that I am unable to retrieve items from local storage during the document.ready, but I am able to get the items afterwards.
我相信你需要使用“setItem”方法,即:
I believe you need to use the "setItem" method, i.e.:
看看我的代码
你不需要 getItem 和 setItem
Have a look at my code
You do not need getItem and setItem