据浏览器自动选择使用 localStorage、globalStorage 或者 userData实现本地存储
项目地址:https://github.com/marcuswestin/store.js
store.js 是一个兼容所有浏览器的 LocalStorage 包装器,不需要借助 Cookie 或者 Flash。store.js 即可实现本地存储功能
store.js - 轻松实现本地存储(LocalStorage)
store.js 是一个兼容所有浏览器的 LocalStorage 包装器,不需要借助 Cookie 或者 Flash。store.js 会根据浏览器自动选择使用 localStorage、globalStorage 或者 userData 来实现本地存储功能。
store.js 提供非常简洁的 API 来实现跨浏览器的本地存储功能:
Tested in iOS 4+
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
Unsupported browsers 老版本不支持,ie7带特殊meta
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.
Storage limits
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
例子
// Store 'marcus' at 'username'
store.set('username', 'marcus')
// Get 'username'
store.get('username')
// Remove 'username'
store.remove('username')
// Clear all keys
store.clear()
// Store an object literal - store.js uses JSON.stringify under the hood
store.set('user', { name: 'marcus', likes: 'javascript' })
// Get the stored object - store.js uses JSON.parse under the hood
var user = store.get('user')
alert(user.name + ' likes ' + user.likes)
// Get all stored values
store.getAll().user.name == 'marcus'
// Loop over all stored values
store.forEach(function(key, val) {
console.log(key, '==', val)
})