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 }();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值