dd

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

jerry thinkphp 2015年11月19日 收藏
jsStorage.js实现对html5的localstorage和sessionStorage的封装操作;
详情请阅读: https://git.oschina.net/wuquanyao/JsStorage
/*+==============================================
  + 我不只是一个程序员,我更希望用此创造价值
  + author:wuquanyao
  + email:wqynqa@163.com
  * version:1.0.0 
  +==============================================*/
var storage=(function(){
    var Storage = function(type){
        this.storage = null;
        if(typeof('type') === 'undefined' || type === 'local')
            this.storage = window.localStorage;
        else if(type === 'session')
            this.storage = window.sessionStorage;
    }
    Storage.prototype.set=function(key, value){
        this.storage.setItem(key, escape(value));
    }
    Storage.prototype.get=function(key){
        return unescape(this.storage.getItem(key));
    }
    Storage.prototype.remove=function(key){
        this.storage.removeItem(key);
    }
    Storage.prototype.clear=function(){
        this.storage.clear();
    }
    Storage.prototype.key=function(index){
        return this.storage.key(index);
    }
    Storage.prototype.hasKey=function(key)
    {
        for(var i in this.storage){
            if(i === key){
                return true;
            }
        }
        return false;
    }
    Storage.prototype.hasVal=function(value)
    {
        for(var i in this.storage){
            if(unescape(this.storage[i]) === value){
                return true;
            }
        }
        return false;
    }
    Storage.stringify = function(data){
        return JSON.stringify(data);
    }
    Storage.parse = function(data){
        return JSON.parse(data);
    }
    Storage.support = function(){
        if(window.localStorage && window.sessionStorage) return true;
        return false;
    }
   return Storage;
})(window);
dd