hugh 的个人博客

【exception】-IE11调用localstorage 报拒绝访问异常

代码如下

 var LStorage = function () {
        var self = this;
    this.canUse = function () {
      
      return !!window.localStorage;  
    };

    this.setItem = function (key, value) {
        if (self.canUse()) {
            try {
                localStorage.setItem(key, value);
                return true;
            } catch (oException) {
                if (oException.name == 'QuotaExceededError') {
                    return false;
                }
            }
        }
        return false;
    };
    this.getItem = function (key) {
        if (self.canUse()) {
            return localStorage.getItem(key);
        }
        return null;
    };
    this.removeItem = function (key) {
        if (self.canUse()) {
            localStorage.removeItem(key);
        }
    }
};

 

错误:

 

 

解决: 

对调用localstorage处加try... catch...

 

附加

chrome 70.x 偶有报错 

Failed to read the 'localStorage' property from 'Window': Access is denied for this document.

标题:【exception】-IE11调用localstorage 报拒绝访问异常
作者:hugh0524
地址:https://blog.uproject.cn/articles/2019/05/23/1558612211154.html