登录(7天不用密码)
1 script src="../cookie.js"></script> 2 <script> 3 4 class Remember{ 5 constructor(){ 6 this.user = document.getElementById("user"); 7 this.pass = document.getElementById("pass"); 8 this.remember = document.getElementById("remember"); 9 this.btn = document.getElementById("btn"); 10 11 this.init() 12 this.getUser(); 13 } 14 init(){ 15 var that = this; 16 this.btn.onclick = function(){ 17 if(that.remember.checked){ 18 // 存cookie 19 that.setCookie() 20 }else{ 21 // 删cookie 22 that.removeCookie() 23 } 24 } 25 } 26 setCookie(){ 27 var data = { 28 u:this.user.value, 29 p:this.pass.value 30 } 31 setCookie("info",JSON.stringify(data),{ 32 expires:7 33 }) 34 } 35 getUser(){ 36 this.info = JSON.parse(getCookie("info")) 37 this.user.value = this.info.u; 38 this.pass.value = this.info.p; 39 } 40 removeCookie(){ 41 removeCookie("info"); 42 } 43 } 44 45 new Remember; 46 47 </script>
body页面
1 <body> 2 用户名:<input type="text" id="user"><br> 3 密码:<input type="text" id="pass"><br> 4 <label for="remember"><input type="checkbox" id="remember">记住密码七天</label> 5 <input type="button" id="btn" value="登录"> 6 </body>