js页面缓存类
JavaScript代码
Code
var Storage = new Class({
initialize: function(name){
this.name = name || 'storage';
if (window.ie) {
this.storage = new Element('span').setStyle("behavior","url('#default#userData')").injectInside(document.body);
} else if (globalStorage) {
this.storage = globalStorage[location.hostname];
}
},
get: function(key) {
if (window.ie) {
this.storage.load('storage');
return Json.evaluate('(' + this.storage.getAttribute(key) + ')');
} else if (this.storage) {
var data = Json.evaluate('(' + this.storage.getItem(this.name) + ')');
return (data) ? data[key] : null;
} else {
return false;
}
},
set: function(key, value) {
if (window.ie) {
this.storage.setAttribute(key, Json.toString(value));
this.storage.save('storage');
} else if (this.storage) {
var data = Json.evaluate('(' + this.storage.getItem(this.name) + ')');
if (!data) data = {};
data[key] = value;
this.storage.setItem(this.name, Json.toString(data));
} else {
return false;
}
return true;
},
clear: function(key) {
if (window.ie) {
this.storage.removeAttribute(key);
this.storage.save('storage');
} else if (this.storage) {
var data = Json.evaluate('(' + this.storage.getItem(this.name) + ')');
if (data) data[key] = undefined;
this.storage.setItem(this.name, Json.toString(data));
} else {
return false;
}
return true;
}
});