IE中未定义localStorage对象

时间:2010-08-02 21:43:43

标签: javascript local-storage internet-explorer-9

我在我的JS应用程序中使用localStorage,我想知道为什么IE9声称localStorage == undefined。据我所知,IE8支持它,有没有办法让它在新版本中运行?

3 个答案:

答案 0 :(得分:57)

您是否在本地HTML文件上进行测试?即file:///网址?

localStorage仅适用于HTTP网站。这在IE9 Dev Preview中没有改变。

答案 1 :(得分:16)

IE 11作品

您只需要两个将文件://127.0.0.1添加到安全性下的受信任区域 选项卡(注意:确保未选中https复选框) 将此行添加到顶部或脚本中,具体取决于您可能不需要的代码,除非您无法连接到互联网。



!localStorage && (l = location, p = l.pathname.replace(/(^..)(:)/, "$1$$"), (l.href = l.protocol + "//127.0.0.1" + p));






!localStorage && (l = location, p = l.pathname.replace(/(^..)(:)/, "$1$$"), (l.href = l.protocol + "//127.0.0.1" + p));

if (typeof(Storage) != "undefined") {
    // Store
    localStorage.setItem("lastname", "Smith");
    // Retrieve
    alert(localStorage.getItem("lastname"));
} else {
    alert("Sorry, your browser does not support Web Storage...");
}




答案 2 :(得分:4)

尝试像这样打开文件

文件://127.0.0.1/c$/pathtofile/file.html