码迷,mamicode.com
首页 > 编程语言 > 详细

【javascript】js本地保存数据的一个解决方案:localStorage

时间:2015-02-28 21:46:51      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:js   js保存数据   多个页面间的数据保持   localstorage   

‘localStorage‘ : localData = {hname : location.hostname ? location.hostname+‘/marchsoft/index.php/OA/‘ : ‘localStatus‘,isLocalStorage : window.localStorage ? true : false,dataDom : null,initDom:function(){ //初始化userDataif(!this.dataDom){try{this.dataDom = document.createElement(‘input‘);//这里使用hidden的input元素this.dataDom.type = ‘hidden‘;this.dataDom.style.display = "none";this.dataDom.addBehavior(‘#default#userData‘);//这是userData的语法document.body.appendChild(this.dataDom);var exDate = new Date();exDate = exDate.getDate()+30;this.dataDom.expires = exDate.toUTCString();//设定过期时间}catch(ex){return false;}}return true;},set:function(key,value){if(this.isLocalStorage){window.localStorage.setItem(key,value);}else{if(this.initDom()){this.dataDom.load(this.hname);this.dataDom.setAttribute(key,value);this.dataDom.save(this.hname)}}},get:function(key){if(this.isLocalStorage){return window.localStorage.getItem(key);}else{if(this.initDom()){this.dataDom.load(this.hname);return this.dataDom.getAttribute(key);}}},add:function(key,value){ var before = this.get(key); if(before != null){ var add = true; var i = 0; for(i;i<before.length;++i){ if(value == this.get(key)[i]){ add = false; } } if(add){ this.set(key,new Array(this.get(key),value)); } }else{ this.set(key,new Array(value)); }},remove:function(key){if(this.isLocalStorage){localStorage.removeItem(key);}else{if(this.initDom()){this.dataDom.load(this.hname);this.dataDom.removeAttribute(key);this.dataDom.save(this.hname);}}}}

【javascript】js本地保存数据的一个解决方案:localStorage

标签:js   js保存数据   多个页面间的数据保持   localstorage   

原文地址:http://blog.csdn.net/u014290054/article/details/43988699

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!