javascript原生Html5本地存储之jsStorage.js

jerry thinkphp 2015年11月19日 收藏
jsStorage.js实现对html5的localstorage和sessionStorage的封装操作;
详情请阅读: https://git.oschina.net/wuquanyao/JsStorage
  1. /*+==============================================
  2.   + 我不只是一个程序员,我更希望用此创造价值
  3.   + author:wuquanyao
  4.   + email:wqynqa@163.com
  5.   * version:1.0.0 
  6.   +==============================================*/
  7. var storage=(function(){
  8.     var Storage = function(type){
  9.         this.storage = null;
  10.         if(typeof('type') === 'undefined' || type === 'local')
  11.             this.storage = window.localStorage;
  12.         else if(type === 'session')
  13.             this.storage = window.sessionStorage;
  14.     }
  15.     Storage.prototype.set=function(key, value){
  16.         this.storage.setItem(key, escape(value));
  17.     }
  18.     Storage.prototype.get=function(key){
  19.         return unescape(this.storage.getItem(key));
  20.     }
  21.     Storage.prototype.remove=function(key){
  22.         this.storage.removeItem(key);
  23.     }
  24.     Storage.prototype.clear=function(){
  25.         this.storage.clear();
  26.     }
  27.     Storage.prototype.key=function(index){
  28.         return this.storage.key(index);
  29.     }
  30.     Storage.prototype.hasKey=function(key)
  31.     {
  32.         for(var i in this.storage){
  33.             if(=== key){
  34.                 return true;
  35.             }
  36.         }
  37.         return false;
  38.     }
  39.     Storage.prototype.hasVal=function(value)
  40.     {
  41.         for(var i in this.storage){
  42.             if(unescape(this.storage[i]) === value){
  43.                 return true;
  44.             }
  45.         }
  46.         return false;
  47.     }
  48.     Storage.stringify = function(data){
  49.         return JSON.stringify(data);
  50.     }
  51.     Storage.parse = function(data){
  52.         return JSON.parse(data);
  53.     }
  54.     Storage.support = function(){
  55.         if(window.localStorage && window.sessionStorage) return true;
  56.         return false;
  57.     }
  58.    return Storage;
  59. })(window);