IE中未定义localStorage对象

Mik*_*ite 28 javascript local-storage internet-explorer-9

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

Ale*_*min 58

您是否在本地HTML文件上进行测试?即一个file:///URL?

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


小智 17

IE 11的工作原理

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

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

!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...");
}
Run Code Online (Sandbox Code Playgroud)


Gon*_*nza 5

尝试像这样打开文件

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

  • @daylight 如果您还将 `file://127.0.0.1` 添加到受信任站点列表中,它会起作用。 (3认同)