Javascript localStorage 对象在 IE 中未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3392032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
localStorage object is undefined in IE
提问by Mikulas Dite
I'm using localStorage in my JS application and I was wondering why IE9 claims localStorage == undefined. As far as I know, IE8 supports it, is here any way to get it working in the new version?
我在我的 JS 应用程序中使用 localStorage,我想知道为什么 IE9 声明localStorage == undefined. 据我所知,IE8 支持它,这里有什么方法可以让它在新版本中工作吗?
回答by Alex Jasmin
Are you testing this on a local HTML file? i.e. a file:///URL?
你是在本地 HTML 文件上测试这个吗?即一个file:///网址?
localStorage is only available on HTTP websites. That hasn't changed in IE9 Dev Preview.
localStorage 仅在 HTTP 网站上可用。这在 IE9 Dev Preview 中没有改变。
回答by user4822973
IE 11 WORKS
IE 11 工程
All you need two do add file://127.0.0.1 to the trusted zones under the security tab (NOTE: make sure https check box IS not checked) add this line to the top or your script, depending on your code you may not need to unless you get could not connect to the internet.
您只需要两个就可以将 file://127.0.0.1 添加到安全选项卡下的受信任区域(注意:确保未选中 https 复选框)将此行添加到顶部或您的脚本,具体取决于您的代码,您可能不会除非您无法连接到互联网,否则需要。
!localStorage && (l = location, p = l.pathname.replace(/(^..)(:)/, "$$"), (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...");
}
回答by Gonza
Try to open the file like this
尝试像这样打开文件
file://127.0.0.1/c$/pathtofile/file.html
file://127.0.0.1/c$/pathtofile/file.html

