<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <title>js COOKIE Remember the account number or password </title> ; script language= "javascript" type= "Text/javascript" > Function onloginloaded () {if (IsPostBack = "False") {Getla Stuser (); } function Getlastuser () {var id = ' 49bac005-7d5b-4231-8cea-16939beacd67 '; var usr = getcookie (id); if (usr!= null) {document.getElementById (' txtUserName '). value = usr; else {document.getElementById (' txtUserName '). Value = "001"; } getpwdandchk (); //Click on login to trigger client event function Setpwdandchk () {//fetch username var usr = document.getElementById (' txtUserName '). Value; alert (USR); Writes the last user information to the cookie setlastuser (USR); If you remember that the password option is selected if (document.getElementById (' chkrememberpwd '). Checked = = true) {//password value var pwd = document.getElementById (' Txtpassword '). Value; Alert (PWD); var expdate = new Date (); EXpdate.settime (Expdate.gettime () + 14 * (24 * 60 * 60 * 1000)); Writes the username and password to the cookie Setcookie (usr,pwd, expdate); else {//If remember password is not checked, expire immediately resetcookie (); } function Setlastuser (usr) {var id = ' 49bac005-7d5b-4231-8cea-16939beacd67 '; var expdate = new Date (); Current time plus two weeks expdate.settime (Expdate.gettime () + 14 * (24 * 60 * 60 * 1000)); Setcookie (id, usr, expdate); The method function Getpwdandchk () {var usr = document.getElementById (' txtUserName ') is called when the user name loses focus. value; var pwd = GetCookie (usr); if (pwd!= null) {document.getElementById (' chkrememberpwd '). Checked = true; document.getElementById (' Txtpassword '). Value = pwd; else {document.getElementById (' chkrememberpwd '). checked = false; document.getElementById (' Txtpassword '). Value = ""; }///Fetch cookie value function GetCookie (name) {var arg = name + ' = '; var alen = Arg.length; var clen = document.cookie.length; var i = 0; while (I < Clen) {var j = i + Alen; Alert (j); if (Document.cookie.substRing (i, j) = = arg) return Getcookieval (j); i = Document.cookie.indexOf ("", I) + 1; if (i = = 0) break; return null; var isPostBack = "<%= isPostBack%>"; function Getcookieval (offset) {var endstr = document.cookie.indexOf (";", offset); if (endstr = = 1) endstr = document.cookie.length; Return unescape (document.cookie.substring (offset, endstr)); //write to Cookie function Setcookie (name, value, expires) {var argv = setcookie.arguments; In this case length = 3 var argc = SetCookie.arguments.length; var expires = (argc > 2)? ARGV[2]: null; var path = (argc > 3)? ARGV[3]: null; var domain = (argc > 4)? ARGV[4]: null; var secure = (argc > 5)? ARGV[5]: false; Document.cookie = name + "=" + Escape (value) + ((expires = null)? "" : ("; Expires= "+ expires.togmtstring ())) + ((path = null)? "" : ("; Path= "+ path") + ((domain = null)? "" : ("; domain= "+ domain") + ((secure = = True)? "; Secure ":" "); function Resetcookie () {var usr = document.getElementById (' txtUserName '). Value; var expdate = new Date (); Setcookie (usr, null, expdate); } </script> </pead> <body onload= "onloginloaded ()" > <form id= "Form1" > <div> username: <in Put type= "text" id= "txtUserName" onblur= "Getpwdandchk ()" > <input type= "password" id= "Txtpassword" > Password:< Input type= "checkbox" id= "chkrememberpwd"/> Remember password <input type= "button" onclick= "Setpwdandchk ()" value= "Enter"/> & lt;/div> </form> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Test method: Join first enter username jb51 password is www.jb51.net, choose Remember Password, then close, reopen page, enter JB51 in username, then password will enter password automatically.