检测不同浏览器中的localStorage v sessionStorage对象(Detecting localStorage v sessionStorage objects in differen

编程入门 行业动态 更新时间:2024-10-25 01:21:07
检测不同浏览器中的localStorage v sessionStorage对象(Detecting localStorage v sessionStorage objects in different browsers)

我正在编写一个能够使用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; }

更多推荐

本文发布于:2023-07-31 00:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340454.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:器中   对象   localStorage   browsers   sessionStorage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!