JS Read and write cookies to realize the code (JS read and write cookies) _javascript skills to remember the account number or password

Source: Internet
Author: User
<! 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.