A localStorage wrapper for all browsers without using cookies or flash. Uses localStorage, globalStorage, and userData behavior under the hood
store.js exposes a simple API for cross browser local storage
store.js uses localStorage when available, and falls back on the userData behavior in IE6 and IE7. No flash to slow down your page load. No cookies to fatten your network requests.
store.js depends on JSON for serialization to disk.
If your product depends on store.js, you must check the
store.enabled flag first:
LocalStorage may sometimes appear to be available but throw an error when used. An example is Safari's private browsing mode. Other browsers allow the user to temporarily disable localStorage. Store.js detects these conditions and sets the
store.enabled flag appropriately.
Introductory Screencast to Store.js by Jack Franklin.
store.js works as expected in node.js, assuming that global.localStorage has been set:
global.localStorage = require('localStorage')var store = require('./store')store.set('foo', 1)console.log(store.get('foo'))
- Tested in iOS 4
- Tested in iOS 5
- Tested in iOS 6
- Tested in Firefox 3.5
- Tested in Firefox 3.6
- Tested in Firefox 4.0+
- Support dropped for Firefox < 3.5 (see notes below)
- Tested in Chrome 5
- Tested in Chrome 6
- Tested in Chrome 7
- Tested in Chrome 8
- Tested in Chrome 10
- Tested in Chrome 11+
- Tested in Safari 4
- Tested in Safari 5
- Tested in IE6
- Tested in IE7
- Tested in IE8
- Tested in IE9
- Tested in IE10
- Tested in Opera 10
- Tested in Opera 11
- Tested in Opera 12
- Tested in Node.js v0.10.4 (with https://github.com/coolaj86/node-localStorage 1.0.2)
Private mode Store.js may not work while browsing in private mode. This is as it should be. Check the
store.enabled flag before relying on store.js.
Saucelabs.com rocks Extensive browser testing of store.js is possible thanks to Saucelabs.com. Check them out, they're awesome.
Firefox 3.0 & 2.0: Support for FF 2 & 3 was dropped in v1.3.6. If you require support for ancient versions of FF, use v1.3.5 of store.js.
Important note: In IE6 and IE7, many special characters are not allowed in the keys used to store any key/value pair. With @mferretti's help, there's a suitable workaround which replaces most forbidden characters with "___".
- IE6 & IE7: 1MB total, but 128kb per "path" or "document" (see http://msdn.microsoft.com/en-us/library/ms531424(v=vs.85).aspx)
- See http://dev-test.nemikor.com/web-storage/support-test/ for a list of limits per browser
- Firefox 1.0: no means (beside cookies and flash)
- Safari 2: no means (beside cookies and flash)
- Safari 3: no synchronous api (has asynch sqlite api, but store.js is synch)
- Opera 9: don't know if there is synchronous api for storing data locally
- Firefox 1.5: don't know if there is synchronous api for storing data locally
- Microsoft IIS & IE7: With meta tag & "charset=iso-8859-1", things stop working. See issue #47.
localStorage, when used without store.js, calls toString on all stored values. This means that you can't conveniently store and retrieve numbers, objects or arrays:
What we want (and get with store.js) is
Some browsers do not have native support for JSON. For those browsers you should include [JSON.js](non-minified copy is included in this repo).
No. I believe there is no way to provide sessionStorage semantics cross browser. However, it is trivial to expire values on read on top of store.js:
var storeWithExpiration =storesetkey val:val exp:exp time:getTimevar info = storegetkeyif !info return nullif getTime - info.time > infoexp return nullreturn infovalstoreWithExpirationset'foo' 'bar' 1000setTimeout console.logstoreWithExpirationget'foo' 500 // -> "bar"setTimeout console.logstoreWithExpirationget'foo' 1500 // -> null
For a browser: Go to http://marcuswestin.github.io/store.js/test.html to test the latest version of store.js.
For a browser, locally: do
npm install node-static && ./node_modules/node-static/bin/cli.js and go to http://localhost:8080
(Note that test.html must be served over http:// or https://. This is because localStore does not work in some browsers when using the file:// protocol.)
For Nodejs: do
npm install . localStorage && node test-node.js