首页 > web前端 > js教程 > 正文

改版了网上的一个js操作userdata_javascript技巧

WBOY
发布: 2016-05-16 19:14:12
原创
1005 人浏览过

复制代码 代码如下:


下面是其它网友文章:
js 保存大量数据.
IE系列存储到电脑上,直到删除文件.
FF关闭则删除
复制代码 代码如下:

<script> <BR>//userData类 <BR>var RuserData={ <BR>o:null, <BR>timeExpires:7, <BR>browser:'', <BR>//初始化userdata <BR>init:function() <BR>{ <BR>if(this.browser != '') <BR>return true; <BR>if(window.ActiveXObject)//for IE { <BR>try{ <BR>this.o=document.createElement("input"); <BR>this.o.id="Ruserdata"; <BR>this.o.type="hidden"; <BR>this.o.style.behavior="url(#default#userData)"; <BR>this.browser='ie'; <BR>document.body.appendChild(this.o); <BR>return true; <BR>}catch(e){return false;} <BR>}else if(window.sessionStorage)//FF 2.0+*/ <BR>{ <BR>try{ <BR>this.o=window.sessionStorage; <BR>this.browser='ff'; <BR>return true; <BR>}catch(e){return false;} <BR>} <BR>return false; <BR>}, <BR>setVal:function(key,val,timeout/*正定义超时时间,为空用默认值*/){ <BR>this.init(); <BR>if(this.browser == 'ie'){ <BR>try{ <BR>this.o.load(key); <BR>this.o.expires=typeof timeout == 'undefined' ? this.getTimeLimit(this.timeExpires) : this.getTimeLimit(timeout); <BR>this.o.setAttribute('value',val); <BR>this.o.save(key); <BR>return true; <BR>}catch(e){return false} <BR>}else if(this.browser == 'ff'){ <BR>try{this.o.setItem(key,val);return true;} <BR>catch(e){return false} <BR>} <BR>}, <BR>getVal:function(key){ <BR>this.init(); <BR>if(this.browser == 'ie'){ <BR>try{ <BR>this.o.load(key); <BR>return this.o.getAttribute('value'); <BR>}catch(e){return false;} <BR>}else if(this.browser == 'ff'){ <BR>try{return this.o.getItem(key);} <BR>catch(e){return false} <BR>} <BR>}, <BR>delVal:function(key){ <BR>this.init(); <BR>if(this.browser == 'ie'){ <BR>try{ <BR>this.o.load(key); <BR>this.o.expires=new Date(315532799000).toUTCString(); <BR>this.o.save(key); <BR>return true; <BR>} <BR>catch(e){return false;} <BR>}else if(this.browser == 'ff'){ <BR>try{this.o.removeItem(key);return true;} <BR>catch(e){return false} <BR>} <BR>}, <BR>getTimeLimit:function(days/*设置超时天数*/){ <BR>var d=new Date(); <BR>d.setDate(d.getDate() + days); <BR>return d.toUTCString(); <BR>} <BR>}; <BR>window.onload=function(){ <BR>//alert(RuserData.delVal('renchao')); <BR>//alert(RuserData.setVal('renchao','脚本之家www.jb51.net')); <BR>alert(RuserData.getVal('renchao')); <BR>} <BR></script>

使用方法:
复制代码 代码如下:

window.onload=function(){
//alert(RuserData.delVal('renchao'));
//alert(RuserData.setVal('renchao','脚本之家www.jb51.net'));
alert(RuserData.getVal('renchao'));
}
相关标签:
js
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!