JavaScript remembers username and login password (two ways) _javascript tips

Source: Internet
Author: User
Tags guid identifier setcookie

The following is mainly through the code to show you the JavaScript memory user name and login password, specific code content please see below.

The first way:

CONTENT
Login.html
Welcome.html
Cookie.js
Common.js

login.html

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  

welcome.html

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 
function $ (objstr) {return document.getelementbyidx_x_x (OBJSTR);} window.onload = function () {//Analysis cookie value, display last login information VA
   R usernamevalue = Getcookievalue ("UserName");
  $ ("UserName"). Value = Usernamevalue;
   var passwordvalue = getcookievalue ("password");  
  $ ("password"). Value = Passwordvalue;
    Write Click event $ ("Submit"). onclick = function () {var usernamevalue = $ ("UserName"). Value;
    var Passwordvalue = $ ("password"). Value;
    Server Authentication (analog) var isadmin = Usernamevalue = = "Admin" && passwordvalue = = "123456";
    var Isusera = Usernamevalue = = "UserA" && passwordvalue = = "UserA"; var ismatched = ISAdmin | |
    Isusera;
         if (ismatched) {if ($ ("Savecookie"). Checked) {Setcookie ("UserName", $ ("UserName"). value,24, "/");
       Setcookie ("Password", $ ("password"). value,24, "/");
       Alert ("Landing successful, welcome you," + Usernamevalue + "!");
     Self.location.replace ("welcome.html"); else alert ("User name or password is wrong, please re-enter!")  
   ");
 }
}

The second way:

<script type= "Text/javascript" > Window.onload=function onloginloaded () {if (IsPostBack = = "False") {Getlastuser (
);  The function Getlastuser () {var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";//guid identifier 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);
The last user information is written 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 weeksTime 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 user name loses focus. value; var pwd = GetCookie (US
R); 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);
= 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.coo
Kie.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 = Setcoo
Kie.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>  

The above is to display JavaScript in two ways to remember the user name and login password of all the code, did not have time to clean up the operation of the chart, I hope you can enjoy.

Related Article

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.