返回顶部

收藏

HTML5 LocalStorage Util

更多
var Util = {
    support: {
        pjax: window.history && window.history.pushState && window.history.replaceState && !navigator.userAgent.match(/(iPod|iPhone|iPad|WebApps\\/.+CFNetwork)/),
        storage: !!window.localStorage
    },
    toInt: function (obj) {
        return parseInt(obj);
    },
    stack: {},
    getTime: function () {
        return new Date * 1;
    },
    // URLhash,pjax=true
    getRealUrl: function (url) {
        url = (url || '').replace(/\\#.*?$/, '');
        url = url.replace('?pjax=true', '').replace('&pjax=true', '');
        return url;
    },
    // urlhash
    getUrlHash: function (url) {
        return url.replace(/^[^\\#]*(?:\\#(.*?))?$/, '$1');
    },
    // key
    getLocalKey: function (src) {
        var s = 'pjax_' + encodeURIComponent(src);
        return {
            data: s + '_data',
            time: s + '_time',
            title: s + '_title'
        };
    },
    // cache
    removeAllCache: function () {
        if (!Util.support.storage)
            return;
        for (var name in localStorage) {
            if ((name.split('_') || [''])[0] === 'pjax') {
                delete localStorage[name];
            }
        }
    },
    // cache
    getCache: function (src, time, flag) {
        var item, vkey, tkey, tval;
        time = Util.toInt(time);
        if (src in Util.stack) {
            item = Util.stack[src], ctime = Util.getTime();
            if ((item.time + time * 1000) > ctime) {
                return item;
            } else {
                delete Util.stack[src];
            }
        } else if (flag && Util.support.storage) { // localStorage
            var l = Util.getLocalKey(src);
            vkey = l.data;
            tkey = l.time;
            item = localStorage.getItem(vkey);
            if (item) {
                tval = Util.toInt(localStorage.getItem(tkey));
                if ((tval + time * 1000) > Util.getTime()) {
                    return {
                        data: item,
                        title: localStorage.getItem(l.title)
                    };
                } else {
                    localStorage.removeItem(vkey);
                    localStorage.removeItem(tkey);
                    localStorage.removeItem(l.title);
                }
            }
        }
        return null;
    },
    // cache
    setCache: function (src, data, title, flag) {
        var time = Util.getTime(), key;
        Util.stack[src] = {
            data: data,
            title: title,
            time: time
        };
        if (flag && Util.support.storage) {
            key = Util.getLocalKey(src);
            localStorage.setItem(key.data, data);
            localStorage.setItem(key.time, time);
            localStorage.setItem(key.title, title);
        }
    },
    // cache
    removeCache: function (src) {
        src = Util.getRealUrl(src || location.href);
        delete Util.stack[src];
        if (Util.support.storage) {
            var key = Util.getLocalKey(src);
            localStorage.removeItem(key.data);
            localStorage.removeItem(key.time);
            localStorage.removeItem(key.title);
        }
    }
};
//该片段来自于http://outofmemory.cn

标签:javascript,网络

收藏

0人收藏

支持

0

反对

0

发表评论