Javascript localStorage 在 Edge 中不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32374875/
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 not working in Edge?
提问by Niels
I am currently working on a some JS. It works fine in every browser apart from Microsoft Edge. The issue is quite simple: at the beginning of one of my scripts I declare a variable like so:
我目前正在研究一些 JS。它在除 Microsoft Edge 之外的所有浏览器中都能正常工作。问题很简单:在我的一个脚本的开头,我声明了一个变量,如下所示:
var something = localStorage.getItem('something');
Anyway the something doesn't exist yet, but the whole idea is that this can be used for reference in a later function. Firefox, Chrome, Opera and Safari don't have a problem with this but Edge does, so my question is, is the a quick fix? Or am i going to have to rewrite my whole script because of Edge?
无论如何,这个东西还不存在,但整个想法是这可以在以后的函数中用作参考。Firefox、Chrome、Opera 和 Safari 没有这个问题,但 Edge 有问题,所以我的问题是,这是一个快速解决方案吗?或者我将不得不因为 Edge 而重写我的整个脚本?
This is the error that edge throws by the way.
这是 edge 顺便抛出的错误。
Unable to get property 'getItem' of undefined or null reference
Thanks!
谢谢!
回答by nils
Local Storage didn't work for local files in IE9, so I imagine that this is still the case in MS Edge.
本地存储不适用于 IE9 中的本地文件,所以我想在 MS Edge 中仍然如此。
I just tested it in Edge with a server on localhost
and your line of code worked just fine:
我刚刚在带有服务器的 Edge 中对其进行了测试localhost
,您的代码行运行良好:
> var something = localStorage.getItem('something');
> undefined
It is possible that this was a security issue in earlier versions of IE and was just never updated as the browser was developed.
这可能是 IE 早期版本中的一个安全问题,并且在开发浏览器时从未更新过。
Although, it appears that localStorage and sessionStorage still don't work in Edge for HTML files accessed using the 'file://' protocol.
尽管如此,对于使用“file://”协议访问的 HTML 文件,localStorage 和 sessionStorage 在Edge 中似乎仍然不起作用。
回答by Vlad Bezden
Could you please try
你能试试吗
var something = window.localStorage.getItem('something');
Could you also check if you have 'Enable DOM Storage' selected? You can find it under: Internet Options -> Advanced tab -> Security group box
您还可以检查是否选择了“启用 DOM 存储”?您可以在以下位置找到它:Internet 选项 -> 高级选项卡 -> 安全组框
Also if you are running your page from local filesystem, localStorage doesn't work on IE, you have to run it from the web server.
此外,如果您从本地文件系统运行页面,则 localStorage 在 IE 上不起作用,您必须从 Web 服务器运行它。
Here is a link that provides more information of how to enable it
回答by Chris
Maybe DOMStorage is turned off? Test with this:
也许 DOMStorage 已关闭?用这个测试:
if (typeof window.Storage === 'undefined') {
alert('Storage turned off...');
}
回答by Tushar Kshirsagar
If someone is looking for solution older version of browser to also work to store key value for using between pages. logic could be
如果有人正在寻找解决方案,旧版本的浏览器也可以存储用于页面之间使用的键值。逻辑可能是
function detectIE() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return false;
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
return false;
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// Edge (IE 12+) => return version number
return false;
}
// other browser
return true;
}
then to set key value use something like this
然后设置键值使用这样的东西
`if (detectIE()) { window.localStorage.setItem('key1', value1);window.localStorage.setItem('key2', value2);}else{ setCookie('key1','value1',1);var value1 = getCookie('key1');}
function setCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;}
also you can erase the cookie
你也可以删除cookie
function eraseCookie(name) {
document.cookie = name+'=; Max-Age=-99999999;'; }