标签:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>自动登陆</title> 6 <script> 7 8 9 function addCookie(_name, _value, _day){ 10 //document.cookie="key=value; expires=time" 11 var d = new Date(); 12 d.setDate(d.getDate()+_day); 13 document.cookie=_name+"="+_value+"; expires="+d.toGMTString(); 14 } 15 16 function getCookie(_name){ 17 var str=""; 18 //document.cookie == key1=v1; key2=v2; key3=v3....... 19 //col[0] == key1=v1 20 //arr[0] == key1 21 var col=document.cookie.split("; "); 22 for(var i in col){ 23 var arr=col[i].split("="); 24 if(arr[0]==_name){ 25 str=arr[1]; 26 break; 27 } 28 } 29 return str; 30 } 31 32 function delCookie(){ 33 addCookie(‘uname‘, ‘‘, -1); 34 addCookie(‘upass‘, ‘‘, -1); 35 document.getElementById("oDiv").style.display="block"; 36 document.getElementById("oUser").style.display="none"; 37 } 38 39 window.onload=function(){ 40 var strName=getCookie("uname"); 41 if(strName!=""){ 42 document.getElementById("oDiv").style.display="none"; 43 document.getElementById("oUser").style.display="block"; 44 document.getElementById("oUser").innerHTML="欢迎您,"+strName+",<span onclick=‘delCookie();‘>退出</span>"; 45 } 46 47 document.getElementById("btn").onclick=function(){ 48 var _uname=document.getElementById("uname").value; 49 var _upass=document.getElementById("upass").value; 50 51 if(document.getElementById("cb").checked){ 52 addCookie("uname", _uname, 1); 53 addCookie("upass", _upass, 1); 54 } 55 56 document.getElementById("oDiv").style.display="none"; 57 document.getElementById("oUser").style.display="block"; 58 document.getElementById("oUser").innerHTML="欢迎您,"+_uname+",<a href=‘javascript:delCookie();‘>退出</a>"; 59 } 60 } 61 </script> 62 </head> 63 <body> 64 <div id="oDiv"> 65 用户名:<input id="uname" type="text" /><br> 66 密码:<input id="upass" type="password" /><br> 67 <input id="btn" type="button" value="登陆" /> 68 <input id="cb" type="checkbox" value="1" />自动登陆 69 </div> 70 <div id="oUser"></div> 71 </body> 72 </html>
标签:
原文地址:http://www.cnblogs.com/thestudy/p/5628059.html