(function(window, undefined){ function userData(){ var doc = document; var name = location.host || ‘localhost‘; var store = doc.createElement(‘div‘); var expires = new Date(); var keys = []; store.style.display = ‘none‘; store.style.behavior = ‘url("#default#userData")‘; doc.body.appendChild(store); expires.setDate(expires.getDate()+365); store.expires = expires.toUTCString(); store.load(name); function getItem(key){ return store.getAttribute(key); } function setItem(key, val){ store.setAttribute(key, val); store.save(name); return this; } function removeItem(key){ store.removeAttribute(key); store.save(name); return this; } return { length: length, getItem: getItem, setItem: setItem, removeItem: removeItem } } function winLocalStorage(store){ function getItem(key){ return store.getItem(key); } function setItem(key,val){ store.setItem(key, val); return store; } function removeItem(key){ store.removeItem(key); return store; } return { getItem: getItem, setItem: setItem, removeItem: removeItem } } window.LocalStorage = window.localStorage && (new winLocalStorage(window.localStorage)) || new userData(); })(window) alert(LocalStorage.setItem(‘aa‘, 123).getItem(‘aa‘));
原文:http://www.cnblogs.com/bjmumu/p/3582118.html