所以我正在玩HTML 5和localStorage,我注意到当我在本地主机上的Firefox中运行页面时,我的值才会被存储(即http://127.0.0.1:8000/test/index. html),但是当我在本地运行文件(file:/// C:/test/index.html)时,我的值不会被存储.Safari 4在两种设置方面都没有问题.那么有人知道这是否是设计 - > Mozilla开发人员中心的DOM存储
(Firefox 2允许访问域层次结构中比当前文档更高的存储对象.出于安全原因,Firefox 3中不再允许这样做.此外,HTML 5规范中的这一提议已经从HTML 5规范中删除了localStorage,在Firefox 3.5中实现.)
或者如果有解决方法?
我不知道因为只能在线工作的离线存储听起来很傻:P
如果有人想知道,代码就像它得到的一样简单:
function save()
{
localStorage.setItem('foo','bar');
}
function load()
{
var test = localStorage.getItem('foo');
alert(test);
}
Run Code Online (Sandbox Code Playgroud) 更改localStorage时应该触发的事件似乎缺少Firefox中的信息.
我设置了以下事件处理程序:
function storageEventHandler(e){
alert("key " + e.key);
alert("oldValue " + e.oldValue);
alert("newValue " + e.newValue);
alert("url " + e.url);
}
window.addEventListener('storage', storageEventHandler, false);
Run Code Online (Sandbox Code Playgroud)
应由此触发:
localStorage.setItem('foo', 'bar');
但是,事件中的所有属性(例如e.key和其他所有属性)都是未定义的.我正在使用Firefox 3.16.为什么事件属性未定义?
编辑.这是我正在使用的所有代码.存储事件在Firefox 3.16中触发,但在Firefox 4.0b8中不触发
此外,重要的是,我从XAMPP运行它http://localhost/index.html 从file运行它://使它死于localStorage获取NULL?
<!DOCTYPE html5>
<html lang="en">
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
$(function() {
var edit = document.getElementById('edit');
$(edit).blur(function() {
localStorage.setItem('todoData', this.innerHTML);
});
// when the page loads
if (localStorage.getItem('todoData')) {
edit.innerHTML = localStorage.getItem('todoData');
}
window.addEventListener('storage', storageEventHandler, false);
function storageEventHandler(e){
alert('localStorage event fired')
}
});
</script>
</head> …Run Code Online (Sandbox Code Playgroud)