奇米影视四色7777久久精品-奇米影视777在线观看一区-七草千岁在线-破产姐妹第二季在线观看天天美剧-国产精品视频铁牛tv-国产精品视频视频久久

0712-2888027 189-8648-0214
微信公眾號(hào)

孝感風(fēng)信網(wǎng)絡(luò)科技有限公司微信公眾號(hào)

當(dāng)前位置:主頁 > 技術(shù)支持 > Javascript/JQuery > 設(shè)置緩存數(shù)據(jù)js源代碼cache.js

設(shè)置緩存數(shù)據(jù)js源代碼cache.js

時(shí)間:2020-10-15來源:風(fēng)信官網(wǎng) 點(diǎn)擊: 1206次
設(shè)置緩存數(shù)據(jù)js源代碼cache.js,可用于在瀏覽器中利用js緩存數(shù)據(jù),需要的小伙伴可以了解一下。
 
cache.js源代碼如下:
 
$.extend($, {
    Cache : {
        userData: false,
        supportLocalStorage: typeof localStorage == 'object' ? true : false,
        name: location.hostname,
 
        init: function () {
            if ( $.Cache.supportLocalStorage )
                return false;
            if ( !$.Cache.userData ) {
                try {
                    $.Cache.userData = document.createElement('INPUT');
                    $.Cache.userData.type = "hidden";
                    $.Cache.userData.style.display = "none";
                    $.Cache.userData.addBehavior("#default#userData");
                    document.body.appendChild($.Cache.userData);
                    var expires = new Date();
                    expires.setDate(expires.getDate() + 365);
                    $.Cache.userData.expires = expires.toUTCString();
                } catch (e) {
                    return false;
                }
            }
            return true;
        },
 
        set: function (key, value, expire) {
            if ( typeof value == 'object' ) {
                value = JSON.stringify(value);
            }
            if ( expire == undefined )
                expire = 0;
 
            if ( $.Cache.init() ) {
                $.Cache.userData.load($.Cache.name);
                $.Cache.userData.setAttribute(key, value);
                if ( expire > 0 ) {
                    var timestamp = Date.parse(new Date());
                    var expiration = timestamp + expire;
                    $.Cache.userData.setAttribute(key + "_EXPIRE", expiration);
                }
                $.Cache.userData.save($.Cache.name);
            } else {
                localStorage.setItem(key, value);
                if ( expire > 0 ) {
                    var timestamp = Date.parse(new Date());
                    var expiration = timestamp + expire;
                    localStorage.setItem(key + "_EXPIRE", expiration);
                }
            }
        },
 
        get: function (key) {
            var val;
            var timestamp = Date.parse(new Date());
            if ( $.Cache.init() ) {
                $.Cache.userData.load($.Cache.name);
                val = $.Cache.userData.getAttribute(key);
                var expiration = $.Cache.userData.getAttribute(key + "_EXPIRE");
                if ( expiration != null && expiration != undefined && expiration > 0  ) {
                    if ( expiration < timestamp) {
                        $.Cache.userData.removeAttribute(key);
                        $.Cache.userData.removeAttribute(key + "_EXPIRE");
                        return undefined;
                    }
                }
            } else {
                val = localStorage.getItem(key);
                var expiration = localStorage.getItem(key + "_EXPIRE");
                if ( expiration != null && expiration != undefined && expiration > 0 ) {
                    if ( expiration < timestamp) {
                        localStorage.removeItem(key);
                        localStorage.removeItem(key + "_EXPIRE");
                        return undefined;
                    }
                }
            }
            if ( val == null || val == undefined || val == "" )
                return undefined;
            if ( val.indexOf("{") == 0 || val.indexOf("[") == 0 ) {
                return JSON.parse(val);
            }
            return val;
        },
        del : function(key) {
            if ( $.Cache.init() ) {
                $.Cache.userData.load($.Cache.name);
                $.Cache.userData.removeAttribute(key);
                $.Cache.userData.removeAttribute(key + "_EXPIRE");
            } else {
                localStorage.removeItem(key);
                localStorage.removeItem(key + "_EXPIRE");
            }
        },
    }
});
 
使用方法演示:
 
$(function(){
var cacheCity = $.Cache.get('city');
        var cacheCity_id = $.Cache.get('city_id');
 
        if ( cacheCity ) {
            $("input[name=city]").val(cacheCity);
        }
 
        if(cacheCity_id){
            $("input[name=city_id]").val(cacheCity_id);
        }
}
 
緩存數(shù)據(jù)可直接賦值給input,使用起來還是非常方便的。
熱門關(guān)鍵詞: 緩存數(shù)據(jù) js 源代碼 cache.js
欄目列表
推薦內(nèi)容
熱點(diǎn)內(nèi)容
展開
主站蜘蛛池模板: 在线观看亚洲欧美| 烈车战队特急者免费观看| www狠狠操| 亚洲精品视频在线看| 亚洲国产激情一区二区三区| 西瓜视频网页版| 草草爱| 日本一区二区三区在线观看| 青青久久精品| jiuyingyuan| 爱爱动图视频| 精品一区狼人国产在线| 三级在线观看| 波多野结衣中文字幕一区二区| 国产一区欧美| 欧美一级淫片aaaaaaa视频| 日韩在线视频中文字幕| 亚洲欧美在线观看视频| 漂亮妈妈| 欧美国产第一页| 亚洲国产精品一区二区久| 免费人成网站在线高清| 精品国产一二三区| 日本a网| 变成黑皮辣妹在线观看| 欧美日韩精品乱国产538| 亚洲美国avcom| 京剧猫第一季| 4hu四虎永久免在线视看| ww在线观视频免费观看| 国产成人精品日本亚洲专| 日韩欧美亚洲综合一区二区| 国产高清一| jizz亚洲| 日本特黄特黄aaaaa大片| 亚洲国产aaa毛片无费看| 亚洲精品乱码久久久久久蜜桃欧美| 日韩毛片一级| 日韩精品一区二区三区在线观看l| 国产中文字幕视频| 国产成人久久|