zoukankan      html  css  js  c++  java
  • 面向对象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;
    }



  • 相关阅读:
    如何使用GOOGLE高级搜索技巧
    你所认为的极限,可能只是别人眼中的起点
    飞机选座——附:东航320选坐攻略
    古诗词里,从初识到相爱到分离到重逢的漫长过程
    从零开始学摄影
    Python之运维
    Linux用户和组密令大全
    centos7 下安装生物信息软件的问题小总结
    VMware锁定文件失败开启模块diskearly的操作失败未能启动虚拟机
    linux 基本命令整理--转
  • 原文地址:https://www.cnblogs.com/sunny123-/p/5987136.html
Copyright © 2011-2022 走看看