JS技术

HTML5之IE下离线存储,可以替代cookie

字号+ 作者: 来源: 2014-11-16 22:20 我要评论( )

此方法可以替代cookie,好处是可以不用和服务器交互,减少服务器负担,方便快捷。好处多多

HTML5之IE下离线存储,可以替代cookie

此方法可以替代cookie,好处是可以不用和服务器交互,减少服务器负担,方便快捷。好处多多啊。

javascript

var ieUserData = function (fileName) {
	this.init(fileName);
};
ieUserData.prototype = {
	ready: true,
	fileName: null,
	o: null,
	expires: 365,
	keys: [],
	length: 0,
	init: function (fileName) {
		try {
			this.fileName = fileName;
			this.o = document.createElement('div');
			this.o.id = "historyUserData";
			this.o.style.display = "block";
			this.o.addBehavior("#default#userData");
			document.body.appendChild(this.o);
		} catch (e) {
			this.ready = false;
		}
		if (this.ready) {
			try {
				this.o.load(this.fileName);
				this.keys = this.o.getAttribute('userdatakeys').split(',');
				this.length = this.keys.length;
			} catch (e) {}
		}
	},
	setItem: function (key, value, expires) {
		if (this.ready) {
			this.keys.push(key);
			this.length = this.keys.length;
			this.o.setAttribute('userdatakeys', this.keys.join(','));  
			this.o.setAttribute(key, value);    
			var d = new Date(),
				e = expires || this.expires;
			d.setDate(d.getDate() + e);
			this.o.expires = d.toUTCString();       
			this.o.save(this.fileName);
		}
	},
	getItem: function (key) {
		if (this.ready) {
			try {
				this.o.load(this.fileName);
				return this.o.getAttribute(key);
			} catch (e) {
				return null;
			}
		}
	},
	removeItem: function(key){
		if (this.ready) {
			try {
				for(var i =0, ilen=this.keys.length, n=0; i<ilen ;i++){
					if(this.keys[i] != key){
						this.keys[n++] = this.keys[i];
					}
				}
				this.keys.length--;
				this.length = this.keys.length;
 
				this.o.load(this.fileName);
				this.o.setAttribute('userdatakeys', this.keys.join(',')); 
				this.o.removeAttribute(key);
				return this.o.save(this.fileName);
			} catch (e) {
				return null;
			}
		}
	},
	clear: function(){
		if (this.ready) {
			var d = new Date();
			d.setDate(d.getDate() -365);
			this.o.expires = d.toUTCString();  
			this.o.save(this.fileName);
			this.keys = [];
			this.length = 0;
		}
	},
	key: function(n){
		return this.keys[n] ? this.keys[n] : null;
	}
};

html代码

C:\Documents and Settings\zero.zhu\UserData\
 
 
<input id="key" type="text" /> 
<textarea id="value" style="width: 300px; height: 200px;"></textarea> 
<input id="save" type="button" value="save" /> 
<input id="get" type="button" value="get" />
<input id="remove" type="button" value="remove" />
<input id="clear" type="button" value="clear" />
<input id="length" type="button" value="length" />
<input id="getkey" type="button" value="getkey" />
<script type="text/javascript"><!--mce:0--></script>

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 老生常谈,JavaScript闭包中的this对象

    老生常谈,JavaScript闭包中的this对象

    2016-02-26 10:21

  • AngularJS使用HTML5摄像头拍照

    AngularJS使用HTML5摄像头拍照

    2016-02-23 09:42

  • 学习JavaScript之this,call,apply

    学习JavaScript之this,call,apply

    2016-01-28 20:45

  • JavaScript复习笔记--字符串

    JavaScript复习笔记--字符串

    2016-01-27 17:16

网友点评