UNPKG

6.26 kBMarkdownView Raw
1store.js
2========
3
4store.js exposes a simple API for cross browser local storage
5
6 // Store 'marcus' at 'username'
7 store.set('username', 'marcus')
8
9 // Get 'username'
10 store.get('username')
11
12 // Remove 'username'
13 store.remove('username')
14
15 // Clear all keys
16 store.clear()
17
18 // Store an object literal - store.js uses JSON.stringify under the hood
19 store.set('user', { name: 'marcus', likes: 'javascript' })
20
21 // Get the stored object - store.js uses JSON.parse under the hood
22 var user = store.get('user')
23 alert(user.name + ' likes ' + user.likes)
24
25store.js depends on JSON for serialization.
26
27How does it work?
28------------------
29store.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.
30
31Screencast
32-----------
33[Introductory Screencast to Store.js](http://javascriptplayground.com/blog/2012/06/javascript-local-storage-store-js-tutorial) by Jack Franklin.
34
35
36`store.enabled` - check that localStorage is available
37-------------------------------------------------------
38To check that persistance is available, you can use the `store.enabled` flag:
39
40 if( store.enabled ) {
41 console.log("localStorage is available");
42 } else {
43 //time to fallback
44 }
45
46Please note that `store.disabled` does exist but is deprecated in favour of `store.enabled`.
47
48There 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.
49
50
51
52Serialization
53-------------
54localStorage, 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:
55
56 localStorage.myage = 24
57 localStorage.myage !== 24
58 localStorage.myage === '24'
59
60 localStorage.user = { name: 'marcus', likes: 'javascript' }
61 localStorage.user === "[object Object]"
62
63 localStorage.tags = ['javascript', 'localStorage', 'store.js']
64 localStorage.tags.length === 32
65 localStorage.tags === "javascript,localStorage,store.js"
66
67What we want (and get with store.js) is
68
69 store.set('myage', 24)
70 store.get('myage') === 24
71
72 store.set('user', { name: 'marcus', likes: 'javascript' })
73 alert("Hi my name is " + store.get('user').name + "!")
74
75 store.set('tags', ['javascript', 'localStorage', 'store.js'])
76 alert("We've got " + store.get('tags').length + " tags here")
77
78The 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.
79
80Some browsers do not have native support for JSON. For those browsers you should include [JSON.js](non-minified copy is included in this repo).
81
82Tests
83-----
84Go to test.html in your browser.
85
86(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)
87
88Supported browsers
89------------------
90 - Tested in Firefox 3.5
91 - Tested in Firefox 3.6
92 - Tested in Firefox 4.0
93 - Tested in Chrome 5
94 - Tested in Chrome 6
95 - Tested in Chrome 7
96 - Tested in Chrome 8
97 - Tested in Chrome 10
98 - Tested in Chrome 11
99 - Tested in Safari 4
100 - Tested in Safari 5
101 - Tested in IE6
102 - Tested in IE7
103 - Tested in IE8
104 - Tested in Opera 10
105 - Opera 10.54
106
107*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.
108
109*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 "___".
110
111Storage limits
112--------------
113 - IE6 & IE7: 1MB total, but 128kb per "path" or "document" (see http://msdn.microsoft.com/en-us/library/ms531424(v=vs.85).aspx)
114 - See http://dev-test.nemikor.com/web-storage/support-test/ for a list of limits per browser
115
116Supported but broken (please help fix)
117--------------------------------------
118 - Chrome 4
119 - Opera 10.10
120
121Unsupported browsers
122-------------------
123 - Firefox 1.0: no means (beside cookies and flash)
124 - Safari 2: no means (beside cookies and flash)
125 - Safari 3: no synchronous api (has asynch sqlite api, but store.js is synch)
126 - Opera 9: don't know if there is synchronous api for storing data locally
127 - Firefox 1.5: don't know if there is synchronous api for storing data locally
128
129Forks
130----
131 - Original: https://github.com/marcuswestin/store.js
132 - Sans JSON support (simple key/values only): https://github.com/cloudhead/store.js
133 - jQueryfied version: https://github.com/whitmer/store.js
134 - Lint.js passing version (with semi-colons): https://github.com/StevenBlack/store.js
135
136TODO
137----
138 - What are the storage capacities/restrictions per browser?
139 - I believe underlying APIs can throw under certain conditions. Where do we need try/catch?
140 - Test different versions of Opera 10.X explicitly
141
142
143 [JSON.js]: http://www.json.org/json2.js
144
145Contributors
146------------
147 - [@marcuswestin](https://github.com/marcuswestin) Marcus Westin (Author)
148 - [@mjpizz](https://github.com/mjpizz) Matt Pizzimenti
149 - [@StevenBlack](https://github.com/StevenBlack) Steven Black
150 - [@ryankirkman](https://github.com/ryankirkman) Ryan Kirkman
151 - [@pereckerdal](https://github.com/pereckerdal) Per Eckerdal
152 - [@manuelvanrijn](https://github.com/manuelvanrijn) Manuel van Rijn
153 - [@StuPig](https://github.com/StuPig) Shou Qiang
154 - [@blq](https://github.com/blq) Fredrik Blomqvist
155 - [@tjarratt](https://github.com/tjarratt) Tim Jarratt
156 - [@gregwebs](https://github.com/gregwebs) Greg Weber
157 - [@jackfranklin](https://github.com/jackfranklin) Jack Franklin
158 - [@pauldwaite](https://github.com/pauldwaite) Paul D. Waite
159 - [@mferretti](https://github.com/mferretti) Marco Ferretti
160 - [@whitehat101](https://github.com/whitehat101) Jeremy Ebler
161 - [@lepture](https://github.com/lepture) Hsiaoming Yang
162 - [@lovejs](https://github.com/lovejs) Ruslan G
163 - [@rmg](https://github.com/rmg) Ryan Graham