public
Created

  • Download Gist
window.name[Cache]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<script>
/*利且window.name来做缓存库,缓存天气及新闻 最终存成的window.name格式为 key1@=val1@@127545353@;key2@=val2@@127889999@;*/
var Cache = (function() {
var cache = {
$: function(x) { return new RegExp('\\b' + x + '@=(.*?)(?=$|@;)') },
get: function(x) { this.$(x).test(window.name); return (RegExp.$1 && RegExp.$1.split('@@')[0]) || null },
set: function(x, val) {
var $ = window, __ = this.$(x), value= x + '@=' + val + '@@' + (+ new Date());
__.test($.name) ? ($.name = $.name.replace(__, value), 1) : ($.name += value + '@;')
},
del: function(x) {
var $ = window, __ = this.$(x);
__.test($.name) && ($.name = $.name.replace(__, '').replace(/(@;)+(?!\w)/g, '$1'));
},
getCacheTime : function(x){
this.$(x).test(window.name); return (RegExp.$1 && RegExp.$1.split('@@')[1]) || null
},
clear: function() { window.name = '' }
};
if (!cache.get('domain') || cache.get('domain') != location.href) {
cache.clear();
cache.set('domain', location.href);
}
return cache;
})();
var ye=1;
window.onload = function() {
if (Cache.get('count') === null) {
Cache.set('count', ye);
Cache.set('haha', 'a' + ye);
} else {
ye = (Cache.get('count') - 0) + 1;
Cache.set('count', ye);
Cache.set('haha', 'a' + ye);
}
aa.innerHTML = Cache.get('count') + ' | ' + Cache.get('haha');
}
</script>
<span id=aa></span>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.