码迷,mamicode.com
首页 > 其他好文 > 详细

userData 本地存储

时间:2014-08-21 18:53:04      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   ar   div   cti   

 

(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‘));

 

userData 本地存储,布布扣,bubuko.com

userData 本地存储

标签:style   blog   color   os   io   ar   div   cti   

原文地址:http://www.cnblogs.com/bjmumu/p/3582118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!