• 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 }();
  • 相关阅读:
    javamail模拟邮箱功能发送电子邮件-基础实战篇(javamail API电子邮件实例)
    javaScript的函数(Function)对象的声明(@包括函数声明和函数表达式)
    java后台调用HttpURLConnection类模拟浏览器请求(一般用于接口调用)
    java 常用concurrent类
    安装php5.5
    Unix 哲学
    mysql创建用户两次授权
    python知识点 07-11
    Gradle 1.3之前的Publishing artifacts
    mysql编码详解
  • 原文地址:https://www.cnblogs.com/goesby/p/4988272.html
Copyright © 2020-2023  润新知