JS技术

html5之离线存储(兼容目前主流浏览器)

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

html5之离线存储(兼容目前主流浏览器)

html5之离线存储(兼容目前主流浏览器

上次发了一个只能兼容IE的版本,今天发一个能兼容所有主流浏览器的版本,IE6+,firefox 3.5+ ,safari 4+ ,chrome5, ie8,都可以完全兼容

/*
** ie event and listener
*/
my.ieUserData = function (fileName) {
	this.init(fileName);
};
my.ieUserData.prototype = {
	_keys: 'userdatakeys',
	ready: true,
	fileName: null,
	o: null,
	expires: 365,
	keys: [],
	length: 0,
	init: function (fileName) {
		try {
			this.fileName = fileName;
			this.o = document.createElement('span');
			this.o.id = "historyUserData";
			this.o.style.display = "none";
			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(this._keys).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(this._keys, 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

 

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

网友点评
>