国产免费又爽又刺激在线观看,免费一级毛片在线播放放视频,最近日本免费观看高清视频,亚洲无码视频观看

<ruby id="cmmwe"></ruby>

  • <var id="cmmwe"></var>
    <b id="cmmwe"></b>
    <source id="cmmwe"><track id="cmmwe"><ins id="cmmwe"></ins></track></source>

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

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

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

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

        時間:2020-10-15來源:風(fēng)信官網(wǎng) 點擊: 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)容
        熱點內(nèi)容
        展開