Wednesday 28 September 2016

javascript - HTML5 Local Storage fallback solutions




I'm looking for javascript libraries and code that can simulate localStorage on browsers that do not have native support.



Basically, I'd like to code my site using localStorage to store data and know that it will still work on browsers that don't natively support it. This would mean a library would detect if window.localStorage exists and use it if it does. If it doesn't exist, then it would create some sort of fallback method of local storage, by creating its own implementation in the window.localStorage namespace.



So far, I've found these solutions:





  1. Simple sessionStorage implementation.

  2. An implementation that uses cookies (not thrilled with this idea).

  3. Dojo's dojox.storage, but it is it's own thing, not really a fallback.



I understand that Flash and Silverlight can be used for local storage as well, but haven't found anything on using them as a fallback for standard HTML5 localStorage. Perhaps Google Gears has this capability too?



Please share any related libraries, resources, or code snippets that you've found! I'd be especially interested in pure javascript or jquery-based solutions, but am guessing that is unlikely.


Answer



I use PersistJS (github repository), which handles client-side storage seamlessly and transparently to your code. You use a single API and get support for the following backends:





  • flash: Flash 8 persistent storage.

  • gears: Google Gears-based persistent storage.

  • localstorage: HTML5 draft storage.

  • whatwg_db: HTML5 draft database storage.

  • globalstorage: HTML5 draft storage (old spec).

  • ie: Internet Explorer userdata behaviors.

  • cookie: Cookie-based persistent storage.




Any of those can be disabled—if, for example, you don't want to use cookies. With this library, you'll get native client-side storage support in IE 5.5+, Firefox 2.0+, Safari 3.1+, and Chrome; and plugin-assisted support if the browser has Flash or Gears. If you enable cookies, it will work in everything (but will be limited to 4 kB).


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...