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

0712-2888027 189-8648-0214
微信公眾號

孝感風信網絡科技有限公司微信公眾號

當前位置:主頁 > 技術支持 > Javascript/JQuery > 設置緩存數據js源代碼cache.js

設置緩存數據js源代碼cache.js

時間:2020-10-15來源:風信官網 點擊: 1206次
設置緩存數據js源代碼cache.js,可用于在瀏覽器中利用js緩存數據,需要的小伙伴可以了解一下。
 
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);
        }
}
 
緩存數據可直接賦值給input,使用起來還是非常方便的。
熱門關鍵詞: 緩存數據 js 源代碼 cache.js
欄目列表
推薦內容
熱點內容
展開
主站蜘蛛池模板: 在线视频区| 亚洲免费网站在线观看| caoprom视频| 韩国久播影院理论片不卡影院| 男女疯狂做爰动态图gif| 国产精品三级视频| 女网址www女高清中国| 日韩 国产 欧美 精品 在线| 成人a大片高清在线观看| 国产xxxx视频| 青草青99久久99九九99九九九| 日产一区二区三区精品视频| 国产精品综合视频| 日韩亚洲欧美在线观看| 亚洲成综合人影院在院播放| 大黄毛片| bt 自拍 另类 综合 欧美| 百乐森林舞会| 日韩欧美视频| 日本a在线看| 寺岛志保| 差不多先生原版| 久久久久久久免费| 亚洲日本欧美产综合在线| heyzo国产亚洲高清| 久久久国产高清| 西瓜视频免费看| 中文字幕亚洲专区| 久久久久久久99视频| 亚洲视频一区在线观看| 国产精品亚洲一区二区三区| 热99re久久精品精品免费| 日韩国产在线观看| 国产黄色免费| 放荡的青春| 青草综合| 欧美精品国产一区二区| www.免费视频| 精品理论片| 91精品在线免费观看| 烈车战队特急者免费观看|