diff --git a/README b/README new file mode 100644 index 00000000..3ee0a3dd --- /dev/null +++ b/README @@ -0,0 +1,45 @@ +store.js +======== + +store.js exposes a simple API for cross browser local store + + // Store 'marcus' at 'username' + store.set('username', 'marcus') + + // Get 'username' + store.get('username') + + // Delete 'username' + store.delete('username') + + // Clear all keys + store.clear() + + // Use JSON to stash an object (see http://www.json.org/json2.js) + store.set('user', JSON.stringify({ name: 'marcus', likes: 'javascript' })) + + // Use JSON to retrieve an object (see http://www.json.org/json2.js) + var user = JSON.parse(store.get('user')) + alert(user.name + ' likes ' + user.likes) + +TODO +---- +I wrote store.js in the past hour looking at https://developer.mozilla.org/en/dom/store and http://msdn.microsoft.com/en-us/library/ms531424.aspx. I haven't tested it yet though. + + - I believe underlying APIs can throw under certain conditions. Where do we need try/catch? + - Write tests + - Test in IE6 + - Test in IE7 + - Test in IE8 + - Test in Firefox 2.0 + - Test in Firefox 3.0 + - Test in Firefox 3.5 + - Test in Firefox 3.6 + - Test in Safari 2 + - Test in Safari 3 + - Test in Safari 4 + - Test in Safari 5 + - Test in Chrome 4 + - Test in Chrome 5 + - Test in Opera 9 + - Test in Opera 10 diff --git a/store.js b/store.js new file mode 100644 index 00000000..c81258e4 --- /dev/null +++ b/store.js @@ -0,0 +1,53 @@ +var store = (function(){ + var api = {}, + win = window, + doc = win.document, + name = 'localStorage', + store + + api.set = function(key, value) {} + api.get = function(key) {} + api.delete = function(key) {} + api.clear = function() {} + + if (win.globalStorage) { + store = win.globalStorage[win.location.hostname] + api.set = function(key, val) { store[key] = val } + api.get = function(key) { return store[key].value } + api.delete = function(key) { delete store[key] } + api.clear = function() { for (var key in store ) { delete store[key] } } + } else if (win.localStorage) { + store = win.localStorage + api.set = function(key, val) { store[key] = val } + api.get = function(key) { return store[key] } + api.delete = function(key) { delete store[key] } + api.clear = function() { for (var key in store ) { delete store[key] } } + } else if (Element.prototype.addBehavior) { + store = doc.body.appendChild(doc.createElement('div')) + store.style.display = 'none' + // See http://msdn.microsoft.com/en-us/library/ms531081(v=VS.85).aspx + // and http://msdn.microsoft.com/en-us/library/ms531424(v=VS.85).aspx + store.addBehavior('#default#userData') + store.load(name) + api.set = function(key, val) { + store.setAttribute(key, val) + store.save(name) + } + api.get = function(key) { + return store.getAttribute(key) + } + api.delete = function(key) { + store.removeAttribute(key) + store.save(name) + } + api.clear = function() { + var attributes = store.XMLDocument.documentElement.attributes; + for (var i=0, attr; attr = attributes[i]; i++) { + store.removeAttribute(attr.name) + } + store.save(name) + } + } + + return api +})();