JavaScript uses cookies to memorize account password features

Source: Internet
Author: User
Tags current time guid identifier setcookie

JavaScript uses cookies to memorize account password features

This article mainly introduced the JavaScript uses the cookie to realize remembers the account password function, this article directly gives the complete test code, needs the friend to be possible to refer to under

Many login functions have a "Remember the password" function, in fact, is nothing more than the reading of cookies.

The following shows the function of the code, the original author has been unable to elegant ....

Test method: Directly Enter account password, submit, refresh the page, and then enter the same account, you can show

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 <! DOCTYPE html> <head> <meta http-equiv= "Content-type" content=; Charset=utf-8 "/> <title>js COOKIE Remember account number or password </title> <script type=" Text/javascript "> Window.onload =function onloginloaded () {if (IsPostBack = = "False") {Getlastuser ();}   function Getlastuser () {var id = 49BA C005-7d5b-4231-8cea-16939beacd67 ";//guid identifier var usr = GetCookie (id); if (usr!= null) {document.getElementById (' txtUserName '). Value = usr;} else {document.getElementById (' txtusername '). VA Lue = "001"; } getpwdandchk (); //Click on login to trigger client event   function Setpwdandchk () {//fetch username var usr = document.getElementById (' txtUserName '). Value alert (US R); 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 a 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 Inter-Expdate.settime (Expdate.gettime () + 14 * (24 * 60 * 60 * 1000)); Setcookie (id, usr, expdate); This method   function Getpwdandchk () {var usr = document.getElementById (' txtUserName ') is called when the username loses focus. value; var pwd = Getco Okie (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.le Ngth; var i = 0; while (I < Clen) {var j = i + Alen;//alert (j); if (Document.cookie.substring (i, j) = = arg) return Getcookieval (j); = 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 = Docum Ent.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> </head>;body> <form id= "Form1" > <div> user name: <input type= "text" id= "txtUserName" onblur= "Getpwdandchk" () "> & Lt;input type= "Password" id= "Txtpassword" > Password: <input type= "checkbox" id= "chkrememberpwd"/> Remember password <input Type= "button" onclick= "Setpwdandchk ()" value= "enter"/> </div> </form> </body> </html>

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.