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