UNPKG

7.28 kBMarkdownView Raw
1store.js
2========
3
4store.js exposes a simple API for cross browser local storage
5
6```js
7// Store 'marcus' at 'username'
8store.set('username', 'marcus')
9
10// Get 'username'
11store.get('username')
12
13// Remove 'username'
14store.remove('username')
15
16// Clear all keys
17store.clear()
18
19// Store an object literal - store.js uses JSON.stringify under the hood
20store.set('user', { name: 'marcus', likes: 'javascript' })
21
22// Get the stored object - store.js uses JSON.parse under the hood
23var user = store.get('user')
24alert(user.name + ' likes ' + user.likes)
25
26// Get all stored values
27store.getAll().user.name == 'marcus'
28```
29
30store.js depends on JSON for serialization.
31
32How does it work?
33------------------
34store.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.
35
36Screencast
37-----------
38[Introductory Screencast to Store.js](http://javascriptplayground.com/blog/2012/06/javascript-local-storage-store-js-tutorial) by Jack Franklin.
39
40
41`store.enabled` - check that localStorage is available
42-------------------------------------------------------
43To check that persistance is available, you can use the `store.enabled` flag:
44
45```js
46if( store.enabled ) {
47 console.log("localStorage is available");
48} else {
49 //time to fallback
50}
51```
52
53Please note that `store.disabled` does exist but is deprecated in favour of `store.enabled`.
54
55There are conditions where localStorage may appear to be available but will throw an error when used. For example, Safari's private browsing mode does this, and some browser allow the user to temporarily disable localStorage. Store.js detects these conditions and sets the `store.enabled` flag accordingly.
56
57
58
59Serialization
60-------------
61localStorage, 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:
62
63```js
64localStorage.myage = 24
65localStorage.myage !== 24
66localStorage.myage === '24'
67
68localStorage.user = { name: 'marcus', likes: 'javascript' }
69localStorage.user === "[object Object]"
70
71localStorage.tags = ['javascript', 'localStorage', 'store.js']
72localStorage.tags.length === 32
73localStorage.tags === "javascript,localStorage,store.js"
74```
75
76What we want (and get with store.js) is
77
78```js
79store.set('myage', 24)
80store.get('myage') === 24
81
82store.set('user', { name: 'marcus', likes: 'javascript' })
83alert("Hi my name is " + store.get('user').name + "!")
84
85store.set('tags', ['javascript', 'localStorage', 'store.js'])
86alert("We've got " + store.get('tags').length + " tags here")
87```
88
89The native serialization engine of javascript is JSON. Rather than leaving it up to you to serialize and deserialize your values, store.js uses JSON.stringify() and JSON.parse() on each call to store.set() and store.get(), respectively.
90
91Some browsers do not have native support for JSON. For those browsers you should include [JSON.js](non-minified copy is included in this repo).
92
93No sessionStorage/auto-expiration?
94----------------------------------
95No. 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:
96
97```js
98var storeWithExpiration = {
99 set: function(key, val, exp) {
100 store.set(key, { val:val, exp:exp, time:new Date().getTime() })
101 },
102 get: function(key) {
103 var info = store.get(key)
104 if (!info) { return null }
105 if (new Date().getTime() - info.time > info.exp) { return null }
106 return info.val
107 }
108}
109storeWithExpiration.set('foo', 'bar', 1000)
110setTimeout(function() { console.log(storeWithExpiration.get('foo')) }, 500) // -> "bar"
111setTimeout(function() { console.log(storeWithExpiration.get('foo')) }, 1500) // -> null
112```
113
114Tests
115-----
116Go to test.html in your browser. (Or http://marcuswestin.github.io/store.js/test.html to test the latest version of store.js)
117
118(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)
119
120Supported browsers
121------------------
122 - Tested in iOS 4
123 - Tested in iOS 5
124 - Tested in iOS 6
125 - Tested in Firefox 3.5
126 - Tested in Firefox 3.6
127 - Tested in Firefox 4.0+
128 - Support dropped for Firefox < 3.5 (see notes below)
129 - Tested in Chrome 5
130 - Tested in Chrome 6
131 - Tested in Chrome 7
132 - Tested in Chrome 8
133 - Tested in Chrome 10
134 - Tested in Chrome 11+
135 - Tested in Safari 4
136 - Tested in Safari 5
137 - Tested in IE6
138 - Tested in IE7
139 - Tested in IE8
140 - Tested in IE9
141 - Tested in IE10
142 - Tested in Opera 10
143 - Tested in Opera 11
144 - Tested in Opera 12
145
146*Saucelabs.com rocks* Extensive browser testing of store.js is possible thanks to Saucelabs.com. Check them out, they're awesome.
147
148*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.
149
150*Important note:* In IE6 and IE7, many special characters are not allowed in the keys used to store any key/value pair. With [@mferretti](https://github.com/mferretti)'s help, there's a suitable workaround which replaces most forbidden characters with "___".
151
152Storage limits
153--------------
154 - IE6 & IE7: 1MB total, but 128kb per "path" or "document" (see http://msdn.microsoft.com/en-us/library/ms531424(v=vs.85).aspx)
155 - See http://dev-test.nemikor.com/web-storage/support-test/ for a list of limits per browser
156
157Unsupported browsers
158-------------------
159 - Firefox 1.0: no means (beside cookies and flash)
160 - Safari 2: no means (beside cookies and flash)
161 - Safari 3: no synchronous api (has asynch sqlite api, but store.js is synch)
162 - Opera 9: don't know if there is synchronous api for storing data locally
163 - Firefox 1.5: don't know if there is synchronous api for storing data locally
164
165Forks
166----
167 - Original: https://github.com/marcuswestin/store.js
168 - Sans JSON support (simple key/values only): https://github.com/cloudhead/store.js
169 - jQueryfied version: https://github.com/whitmer/store.js
170 - Lint.js passing version (with semi-colons): https://github.com/StevenBlack/store.js
171
172 [JSON.js]: http://www.json.org/json2.js
173
174Contributors
175------------
176 - [@marcuswestin](https://github.com/marcuswestin) Marcus Westin (Author)
177 - [@mjpizz](https://github.com/mjpizz) Matt Pizzimenti
178 - [@StevenBlack](https://github.com/StevenBlack) Steven Black
179 - [@ryankirkman](https://github.com/ryankirkman) Ryan Kirkman
180 - [@pereckerdal](https://github.com/pereckerdal) Per Eckerdal
181 - [@manuelvanrijn](https://github.com/manuelvanrijn) Manuel van Rijn
182 - [@StuPig](https://github.com/StuPig) Shou Qiang
183 - [@blq](https://github.com/blq) Fredrik Blomqvist
184 - [@tjarratt](https://github.com/tjarratt) Tim Jarratt
185 - [@gregwebs](https://github.com/gregwebs) Greg Weber
186 - [@jackfranklin](https://github.com/jackfranklin) Jack Franklin
187 - [@pauldwaite](https://github.com/pauldwaite) Paul D. Waite
188 - [@mferretti](https://github.com/mferretti) Marco Ferretti
189 - [@whitehat101](https://github.com/whitehat101) Jeremy Ebler
190 - [@lepture](https://github.com/lepture) Hsiaoming Yang
191 - [@lovejs](https://github.com/lovejs) Ruslan G
192 - [@rmg](https://github.com/rmg) Ryan Graham
193 - [@MatthewMueller](https://github.com/MatthewMueller) Matthew Mueller
194 - [@robinator](https://github.com/robinator) Rob Law
195
\No newline at end of file