• 面向对象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;
    }



  • 相关阅读:
    读书笔记:芳华
    工作4个余月了
    读书笔记:秘密
    读书笔记:移动的帝国_日本移动互联网兴衰启示录
    『用英语学英语』英文语法为什么要有独立主格?
    『转』The Beginning of your Design Career
    『转』Writing Well
    『转』How to Think About Your Career
    selenium RC 环境配置
    菜鸟学自动化测试(一)----selenium IDE
  • 原文地址:https://www.cnblogs.com/sunny123-/p/5987136.html
Copyright © 2020-2023  润新知