|
| 1 | +var store = (function(){ |
| 2 | + var api = {}, |
| 3 | + win = window, |
| 4 | + doc = win.document, |
| 5 | + name = 'localStorage', |
| 6 | + store |
| 7 | + |
| 8 | + api.set = function(key, value) {} |
| 9 | + api.get = function(key) {} |
| 10 | + api.delete = function(key) {} |
| 11 | + api.clear = function() {} |
| 12 | + |
| 13 | + if (win.globalStorage) { |
| 14 | + store = win.globalStorage[win.location.hostname] |
| 15 | + api.set = function(key, val) { store[key] = val } |
| 16 | + api.get = function(key) { return store[key].value } |
| 17 | + api.delete = function(key) { delete store[key] } |
| 18 | + api.clear = function() { for (var key in store ) { delete store[key] } } |
| 19 | + } else if (win.localStorage) { |
| 20 | + store = win.localStorage |
| 21 | + api.set = function(key, val) { store[key] = val } |
| 22 | + api.get = function(key) { return store[key] } |
| 23 | + api.delete = function(key) { delete store[key] } |
| 24 | + api.clear = function() { for (var key in store ) { delete store[key] } } |
| 25 | + } else if (Element.prototype.addBehavior) { |
| 26 | + store = doc.body.appendChild(doc.createElement('div')) |
| 27 | + store.style.display = 'none' |
| 28 | + // See http://msdn.microsoft.com/en-us/library/ms531081(v=VS.85).aspx |
| 29 | + // and http://msdn.microsoft.com/en-us/library/ms531424(v=VS.85).aspx |
| 30 | + store.addBehavior('#default#userData') |
| 31 | + store.load(name) |
| 32 | + api.set = function(key, val) { |
| 33 | + store.setAttribute(key, val) |
| 34 | + store.save(name) |
| 35 | + } |
| 36 | + api.get = function(key) { |
| 37 | + return store.getAttribute(key) |
| 38 | + } |
| 39 | + api.delete = function(key) { |
| 40 | + store.removeAttribute(key) |
| 41 | + store.save(name) |
| 42 | + } |
| 43 | + api.clear = function() { |
| 44 | + var attributes = store.XMLDocument.documentElement.attributes; |
| 45 | + for (var i=0, attr; attr = attributes[i]; i++) { |
| 46 | + store.removeAttribute(attr.name) |
| 47 | + } |
| 48 | + store.save(name) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return api |
| 53 | +})(); |
0 commit comments