奇米影视四色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
欄目列表
推薦內容
熱點內容
展開
主站蜘蛛池模板: 亚洲最大免费视频网| 亚洲国产免费| 青青青草视频| 久久久久免费精品国产| 久久精品国产在热亚洲完整版| 在线 亚洲 欧美| www视频免费看| 国产午夜久久精品| 国产福利一区二区在线观看| 免费一级毛片在播放视频| 最新黄色网址在线观看| 中文字幕视频在线观看| 免费人成网站在线高清| 九九国产视频| 小草高清免费视频| 午夜影院0606免费| 欧美午夜三级我不卡在线观看| 九九精彩视频在线观看视频| 亚洲一区二区三区高清不卡| 99综合视频| 国产精品第7页| 青草娱乐极品免费视频| 久久久久免费| 小蝌蚪视频在线观看免费高清6| 99久久99这里只有免费费精品| 日本精品视频在线播放| after第一部| a视频网站| 高清不卡在线| 国产一级黄色影片| 国精视频一区二区视频| 亚洲国产激情一区二区三区| 欧美午夜大片| 国产成人深夜福利短视频99| 久久国产资源| 亚洲国产成人久久精品图片| 美国一级大黄大色毛片| 青青青视频免费| 国产日韩不卡免费精品视频| 成在线人免费视频一区二区三区| 国产亚洲综合在线|