javascript本地缓存之userData(ie专用)

/**
 *storage 对象
 */
function storage (){
	var args = [].slice.call(arguments);
	this.storeName = location.hostname||"defaultData";
	this.store = {};
	this.inited = false;
}

storage.prototype={
	init:function (){
		var doc = document;
		var _storeEle = doc.createElement("div"),_body = doc.body;
		_storeEle.style.display = "none";
		_storeEle.addBehavior("#default#userData");
		_body.appendChild(_storeEle);
		this.store = _storeEle;
		this.inited = true;
	},
	getData:function (key){
		if(this.inited){
			var _st = this.store;
			_st.load(this.storeName);
			return _st.getAttribute(key);
		}else{
			this.init();
			return arguments.callee.call(this,key);
		}
	},
	addData:function (key,val,expire){
		if(this.inited){
			 var _st = this.store;
			 _st.load(this.storeName);
			 _st.setAttribute(key,val);
			 if(expire){
				 var d = new Date()+expire;
				 _st.expires = d.toUTCString();
			 }
			 _st.save(this.storeName);
		}else{
			this.init();
			arguments.callee.call(this,key,val,expire);
		}
	},
	exist:function (key){
		return !!this.getData(key);
	},
	removeData:function (key){
		var _st = this.store;
		_st.load(this.storeName);
		_st.removeAttribute(key);
		_st.save(this.storeName);
	}
}
var _st = new storage("my");
var _cache = _st.getData("test");
alert(_cache);
_st.addData("test","333");

 

 

你可能感兴趣的:(JavaScript)