简体   繁体   English

HTML5 sessionStorage 是否有提供良好兼容性实现的包装器?

[英]Is there a wrapper for HTML5 sessionStorage that provides a good compatability implementation?

We would like to use the HTML5 sessionStorage (or something like it) in a new project but you can't rely on it on some browsers.我们想在新项目中使用 HTML5 sessionStorage(或类似的东西),但您不能在某些浏览器上依赖它。 There's this project ( http://code.google.com/p/sessionstorage/ ) which provides an implementation that works a lot of places, yet oddly it doesn't use the HTML5 version if it is available.有这个项目( http://code.google.com/p/sessionstorage/ )提供了一个适用于很多地方的实现,但奇怪的是它不使用 HTML5 版本(如果可用)。 It always does its own thing.它总是做自己的事情。

There's this project ( https://github.com/jas-/jQuery.handleStorage ) over on GitHub GitHub 上有这个项目( https://github.com/jas-/jQuery.handleStorage but its emulation layer is cookies (bleh!)但它的仿真层是 cookies (bleh!) . .

Note: The above is incorrect, see the answer from jas- below where he explains that I was mistaken about that project.注意:以上是不正确的,请参阅下面 jas- 的答案,他解释说我对那个项目有误。

Is there anything better out there that offers a good implementation like the first project but falls back automatically to HTML5 if it is available?有没有更好的方法可以像第一个项目一样提供良好的实现,但如果可用,它会自动退回到 HTML5 吗? We might retrofit the first project to automatically fall back to HTML5 if available but would rather not build that if there's already a good solution out there.如果可用,我们可能会 retrofit 第一个自动回退到 HTML5 的项目,但如果那里已经有一个好的解决方案,我们宁愿不构建它。

Munsch,蒙施,

I realize this post is several years old but I wanted to clarify the project you mentioned, I am the author of jQuery.handleStorage and it does not use cookies as its default method of client storage.我意识到这篇文章已经有好几年了,但我想澄清你提到的项目,我是jQuery.handleStorage的作者,它不使用 cookies 作为其默认的客户端存储方法。

If you read the documentation for the project you will see that one of its three client storage options is to use cookies in the event a clients browser does not support the HTML5 localStorage and/or sessionStorage API's.如果您阅读该项目的文档,您将看到它的三个客户端存储选项之一是在客户端浏览器不支持 HTML5 localStorage 和/或 sessionStorage API 的情况下使用 cookies。

With that said, if you need a more robust and flexible client storage tool I would recommend secStore.js as it does not implicitly require binding to a form, supports the HTML5 client storage options, gracefully degrades to cookie in the event of a legacy browser and optionally uses the SJCL API to provide transparent encryption of possible sensitive data.话虽如此,如果您需要更强大和灵活的客户端存储工具,我会推荐secStore.js ,因为它不隐式需要绑定到表单,支持 HTML5 客户端存储选项,在旧版浏览器的情况下优雅地降级为 cookie并可选择使用SJCL API 为可能的敏感数据提供透明加密。

I know you said that you wanted a solution that did it all but what's wrong with something like this?我知道你说过你想要一个解决方案,但这样的事情有什么问题?

if(!window.sessionStorage){
    $.getScript("/google_code_sessionStorage.js");
}

There's a couple of YUI utilities for this, depending on whether you're using 2 or 3:这有几个 YUI 实用程序,具体取决于您使用的是 2 还是 3:

Both will use HTML5 session storage if available.如果可用,两者都将使用 HTML5 session 存储。

I ended up doing more research on this because I liked both of the answers I got, but I wanted something more like the YUI 3: Storage Lite but that didn't require YUI.我最终对此进行了更多研究,因为我喜欢我得到的两个答案,但我想要更像 YUI 3: Storage Lite 的东西,但不需要 YUI。 Something that was framework agnostic or jQuery friendly would work a lot better for us.与框架无关或对 jQuery 友好的东西对我们来说会更好。

In the end I found a couple of possibilities and one stood out as what we'll try and put to use: Lawnchair最后,我发现了几种可能性,其中一种是我们将尝试使用的最突出的:草坪椅

Also considered: store.js , AmplifyJS还考虑: store.jsAmplifyJS

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

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