简体   繁体   English

在不同的浏览器中检测localStorage v sessionStorage对象

[英]Detecting localStorage v sessionStorage objects in different browsers

I'm writing a Javascript class that is able to use either localStorage or sessionStorage. 我正在编写一个能够使用localStorage或sessionStorage的Javascript类。 The selection of this is done on a class instance basis. 选择此操作是基于类实例完成的。

I have a method within this class, which receives the storage object as a parameter and runs an action according to the storage type (ie. local v session). 我在这个类中有一个方法,它接收存储对象作为参数,并根据存储类型(即本地v会话)运行操作。

Eg. 例如。

function myMethod(store){
    // store: object storageObject
    //        The storage object being used (either
    //        sessionStorage or localStorage).

    if(store === sessionStorage){
        return sessionAction(store)
    }else if(store === localStorage){
        return localAction(store)
    }

    return null;
}

This does not work in Internet Explorer 8, producing the error: " Class doesn't support Automation ". 这在Internet Explorer 8中不起作用,产生错误:“ 类不支持自动化 ”。 It seems to work in other browsers quite well. 它似乎在其他浏览器中工作得很好。

I've tried to get the object type (via Object.prototype.toString.call( store ) ) and test against that but IE8 always reports [object Object] for this. 我试图获取对象类型(通过Object.prototype.toString.call( store )并对其进行测试,但IE8总是为此报告[object Object]。 I managed to make some progress from the answer to Stackoverflow question: Weird IE8 internal [[ class ]] attribute behavior . 我设法从Stackoverflow问题的答案中取得了一些进展: 奇怪的IE8内部[[class]]属性行为 This workaround gave me [object Storage] in IE. 这个解决方法在IE中给了我[对象存储]。

However, I still cannot detect between the different storage types. 但是,我仍然无法检测不同的存储类型。 Is there a simple methodology for detecting between the two types that works cross-browser? 是否有一种简单的方法可以检测跨浏览器的两种类型?

I could rewrite it so the type has't to be supplied as a parameter to the method. 我可以重写它,因此不能将类型作为参数提供给方法。 However, I'd rather reduce the simplicity of the API by allowing users to simply supply the storage object. 但是,我宁愿通过允许用户简单地提供存储对象来降低API的简单性。

You can go aggressive for the IE8 code branch (inspired by Modernizr ): 你可以为IE8代码分支积极进取 (受Modernizr的启发):

Compare storages: 比较存储:

function storagesEqual(testStorage, webStorage) {
    try {
        return testStorage === webStorage;
    } catch (ex) {
        // IE8 code branch
        var testKey = "storage-test";
        var testValue = (new Date()).valueOf().toString();
        var result = false;

        try {
            webStorage.setItem(testKey, testValue);
            if(testStorage[testKey] === testValue) {
                webStorage.removeItem(testKey);
                result = true;
            }
        } finally {
            return result;
        }
    }
}

Indetify storage type: Indetify存储类型:

function storageType(store) {

    var STORAGE_LOCAL = 'local';
    var STORAGE_SESSION = 'session';
    var STORAGE_UNKNOWN = 'unknown';

    var localStorage = window.localStorage;
    var sessionStorage = window.sessionStorage;

    if(storagesEqual(store, localStorage)) return STORAGE_LOCAL;
    if(storagesEqual(store, sessionStorage)) return STORAGE_SESSION;
    return STORAGE_UNKNOWN;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM