我正在编写一个能够使用localStorage或sessionStorage的Javascript类。 选择此操作是基于类实例完成的。
我在这个类中有一个方法,它接收存储对象作为参数,并根据存储类型(即本地v会话)运行操作。
例如。
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; }这在Internet Explorer 8中不起作用,产生错误:“ 类不支持自动化 ”。 它似乎在其他浏览器中工作得很好。
我试图获取对象类型(通过Object.prototype.toString.call( store ) )并对其进行测试,但IE8始终报告[object Object]。 我设法从Stackoverflow问题的答案中取得了一些进展: 奇怪的IE8内部[[class]]属性行为 。 这个解决方法给了我IE中的[对象存储]。
但是,我仍然无法检测不同的存储类型。 是否有一种简单的方法可以检测跨浏览器的两种类型?
我可以重写它,因此不能将类型作为参数提供给方法。 但是,我宁愿通过允许用户简单地提供存储对象来降低API的简单性。
I'm writing a Javascript class that is able to use either localStorage or sessionStorage. 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).
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". 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. I managed to make some progress from the answer to Stackoverflow question: Weird IE8 internal [[ class ]] attribute behavior. This workaround gave me [object Storage] in 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.
最满意答案
您可以对IE8代码分支进行攻击 (受Modernizr的启发):
比较存储:
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; } } }适应存储类型:
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; }You can go aggressive for the IE8 code branch (inspired by 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:
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; }更多推荐
发布评论