localStorage not working in Edge?

坚强是说给别人听的谎言 提交于 2019-12-17 19:48:23

问题


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:

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?

This is the error that edge throws by the way.

 Unable to get property 'getItem' of undefined or null reference

Thanks!


回答1:


Local Storage didn't work for local files in IE9, so I imagine that this is still the case in MS Edge.

I just tested it in Edge with a server on localhost and your line of code worked just fine:

> 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.

Although, it appears that localStorage and sessionStorage still don't work in Edge for HTML files accessed using the 'file://' protocol.




回答2:


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

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.

Here is a link that provides more information of how to enable it




回答3:


If someone is looking for solution 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

function eraseCookie(name) {   
document.cookie = name+'=; Max-Age=-99999999;';  }



回答4:


Maybe DOMStorage is turned off? Test with this:

if (typeof window.Storage === 'undefined') {
    alert('Storage turned off...');
}


来源:https://stackoverflow.com/questions/32374875/localstorage-not-working-in-edge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!