面向对象cookie增删查

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="text" name="txt" id="txt" placeholder="用户名" />
<input type="text" name="password" id="password" placeholder="密码" />
<input type="button" name="reset" id="reset" value="重置" />
<input type="button" name="get" id="get" value="获取" />
</body>
<script src="js/cookie.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
function Cookie(){
this.oTxt=document.getElementById("txt");
this.oPd=document.getElementById("password");
this.oReset=document.getElementById("reset");
this.oGet=document.getElementById("get");
}
Cookie.prototype.setCookie=function(){
this.oTxt.onblur=function(){
this.setcookie('user',this.value,10);
}
this.oPd.onblur=function(){
this.setcookie('pwd',this.value,10);
}
}
Cookie.prototype.delCookie=function(){
var This=this;
this.oReset.onclick=function(){
This.oTxt.delcookie('user');
This.oPd.delcookie('pwd');
This.oTxt.value='';
This.oPd.value='';
}
}
Cookie.prototype.getCookie=function(){
var This=this;
this.oGet.onclick=function(){
alert(This.oTxt.getcookie('user'));
alert(This.oPd.getcookie('pwd'));
}
}
var cookie=new Cookie();
cookie.setCookie();
cookie.delCookie();
cookie.getCookie();
</script>
</html>

cookie.js

Object.prototype.setcookie=function( name,val,day){
this.oDate=new Date();//当前时间
this.oDate.setDate(this.oDate.getDate()+day);//过期时间
document.cookie=name+'='+val+';'+'expires='+this.oDate;
}
Object.prototype.delcookie=function( name ){
this.setcookie(name,1,-1);//利用过期时间
}
Object.prototype.getcookie=function( name ){
this.arr=document.cookie.split( '; ' );
for( var i=0;i<this.arr.length;i++ ){
this.arr1=this.arr[i].split('=');
if( this.arr1[0]==name ){
return this.arr1[1];
}
}
return 0;
}



原文地址:https://www.cnblogs.com/sunny123-/p/5987136.html