localStorage.ie6.js
1 !window.localStorage && function() { 2 window.localStorage = {}; 3 var prefix = 'data-userdata' 4 , body = document.body 5 , html = document.documentElement 6 , mark = function(key, isRomove) { // key 值字符串 7 try { 8 html.load(prefix); 9 var tmp = html.getAttribute(prefix); 10 tmp = !tmp ? '' : tmp; 11 } catch(e) { 12 tmp = ''; 13 } 14 15 var reg = tmp.indexOf(key) === 0 ? new RegExp('\\b' + key + '\\b,?', 'i') : new RegExp(',?\\b' + key + '\\b', 'i') 16 , hasKey = reg.test(tmp) ? true : falocalStoragee; 17 18 tmp = isRomove ? tmp.replace(reg, '') : hasKey ? tmp : tmp === '' ? key : tmp.split(',').concat(key).join(','); 19 html.setAttribute(prefix, tmp); 20 html.save(prefix); 21 }; 22 23 body.addBehavior('#default#userData'); 24 html.addBehavior('#default#userData'); 25 26 // getItem() 27 localStorage.getItem = function(key) { 28 try { 29 body.load(key); 30 return body.getAttribute(key); 31 } catch(e) { 32 return null; 33 } 34 }; 35 36 // setItem() 37 localStorage.setItem = function(key, value) { 38 body.setAttribute(key, value); 39 body.save(key); 40 mark(key, false); 41 }; 42 43 // removeItem 44 localStorage.removeItem = function(key) { 45 body.removeAttribute(key); 46 body.save(key); 47 mark(key, true); 48 }; 49 50 // clear() 51 localStorage.clear = function() { 52 try { 53 html.load(prefix); 54 var attrs = html.getAttribute(prefix).split(',') 55 , len = attrs.length; 56 57 for (var i = 0; i < len; i++) { 58 body.removeAttribute(attrs[i]); 59 body.save(attrs[i]); 60 } 61 62 html.setAttribute(prefix, ''); 63 html.save(prefix); 64 } catch(e) { 65 66 } 67 }; 68 }();
如果觉得对你有所帮助就打点一下吧