1
(function (window) {
  localStorage = local_store = {
    getItem: function (skey) {
        if (!skey) {
            return null;
        }
        if (!window.localStorage) {
            return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(skey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1"));
        } else {
            return window.localStorage.getItem(skey);
        }
    },
    key: function (nkeyid) {
        if (!window.localStorage) {
            return unescape(document.cookie.replace(/\s*\=(?:.(?!;))*$/, "").split(/\s*\=(?:[^;](?!;))*[^;]?;\s*/)[nkeyid]);
        } else {
            window.localStorage.key(nkeyid);
        }
    },
    setItem: function (skey, sValue) {
        if (!skey) {
            return;
        }

        if (!window.localStorage) {
            document.cookie = escape(skey) + "=" + escape(sValue) + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/";
            this.length = document.cookie.match(/\=/g).length;
        } else {
            window.localStorage.setItem(skey, sValue);
            this.length++;
        }
    },
    length: (document.cookie.match(/\=/g) || window.localStorage).length,
    removeItem: function (skey) {
        if (skey || !this.hasOwnProperty(skey)) {
            return;
        }
        if (!window.localStorage) {
            document.cookie = escape(skey) + "=;expires=Thu,01 Jan 1970 00:00:00 GMT;path=/";
            this.length--;
        } else {
            window.localStorage.removeItem(skey);
        }
    },
    hasOwnProperty: function (skey) {
        if (!window.localStorage) {
            return (new RegExp("(?:^/;\\s*)" + escape(skey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
        } else {
            return window.localStorage.hasOwnProperty();
        }
    },
    returnObj: function () {
        if (!window.localStorage) {
            return document.cookie;
        } else {
            return window.localStorage;
        }
    }
};
window.local_store = local_store.hasOwnProperty ? local_store.returnObj() : local_store;
}(window));

上面的代码在其他浏览器中运行,但在 IE10 中我收到以下错误:

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

它指向localStorage= local_store = {代码的一部分。

有谁知道为什么会这样?

http://jsbin.com/OdajuGO/1/edit 用于测试目的

4

0 回答 0