封装localStorage js本地存储解决方案

//封装js本地存储解决方案
//localStorage

//方法:
//localStorage.getItem(key):获取指定key本地存储的值

//localStorage.setItem(key, value) :将value存储到key字段

//localStorage.removeItem(key):删除指定key本地存储的值

localData = {
    hname: location.hostname ? location.hostname : 'localStatus',
    isLocalStorage: window.localStorage ? true : false,
    dataDom: null,

    initDom: function () { //初始化userData
        if (!this.dataDom) {
            try {
                this.dataDom = document.createElement('input');//这里使用hidden的input元素
                this.dataDom.type = 'hidden';
                this.dataDom.style.display = "none";
                this.dataDom.addBehavior('#default#userData');//这是userData的语法
                document.body.appendChild(this.dataDom);
                var exDate = new Date();
                exDate = exDate.getDate() + 30;
                this.dataDom.expires = exDate.toUTCString();//设定过期时间
            } catch (ex) {
                return false;
            }
        }
        return true;
    },
    set: function (key, value) {
        if (this.isLocalStorage) {
            window.localStorage.setItem(key, value);
        } else {
            if (this.initDom()) {
                this.dataDom.load(this.hname);
                this.dataDom.setAttribute(key, value);
                this.dataDom.save(this.hname)
            }
        }
    },
    get: function (key) {
        if (this.isLocalStorage) {
            return window.localStorage.getItem(key);
        } else {
            if (this.initDom()) {
                this.dataDom.load(this.hname);
                return this.dataDom.getAttribute(key);
            }
        }
    },
    remove: function (key) {
        if (this.isLocalStorage) {
            localStorage.removeItem(key);
        } else {
            if (this.initDom()) {
                this.dataDom.load(this.hname);
                this.dataDom.removeAttribute(key);
                this.dataDom.save(this.hname)
            }
        }
    }
}

、、、、、使用
//点击搜索结果后储存到本地

    function fnStorage(arr) {

        $(".content ul li").click(function (e) {
            e.stopPropagation(); //阻止  的 click 事件冒泡  
            var obj = {};
            var ntime = new Date().getTime();
            obj.Time = ntime; //时间戳
            obj.EstateName = $(this).find("a").html();//楼盘名称
            obj.EstateID = $(this).attr("EstateID");//楼盘ID
            arr.unshift(obj);
            fnset(arr)
        }) 
    } 
    //   搜索历史的列表组合
    function fnHistory(arr) {
        $(".history ul li").click(function (e) {
            e.stopPropagation(); //阻止  的 click 事件冒泡  
            var obj = {};
            var ntime = new Date().getTime();
            obj.Time = ntime; //时间戳
            obj.EstateName = $(this).find("a").html();//楼盘名称
            obj.EstateID = $(this).attr("EstateID");//楼盘ID
            arr.unshift(obj);
            fnset(arr);
        }) 
    }

// 数组的排序 去重 获取前面十个;

  
    function fnset(arr) {
        if (arr.length > 1) {
            //数组按时间排序

            for (var i = 0; i < arr.length - 1; i++) {
                for (var j = 0; j < arr.length - 1 - i; j++) {
                    var a = arr[j].Time; //转换成时间戳
                    var b = arr[j + 1].Time; //转换成时间戳
                    var c = arr[j].EstateName;// 楼盘名称
                    var d = arr[j + 1].EstateName; // 楼盘名称
                    if (a < b) {
                        var tmp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = tmp;
                    }
                }
            }
            //数组按时间排序 去重
            for (var i = 0; i < arr.length; i++) {
                for (var j = i + 1; j < arr.length; j++) {
                    if (arr[i].EstateName == arr[j].EstateName) {
                        arr.splice(j, 1);
                    }
                }
            } 
        }
        if (arr.length > 10) {
            arr.slice(0, 10);
        }
        var OBJ = { Key: arr };
       var DATA = JSON.stringify(OBJ); 
        localStorage.setItem("key", DATA); //将value存储到key字段

    }

你可能感兴趣的:(封装localStorage js本地存储解决方案)