Use JS to remember the user name and login password effect

Source: Internet
Author: User

<script type= "Text/javascript" >
Window.onload=function onloginloaded () {
if (IsPostBack = = "False") {
Getlastuser ();
}
}

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 ();
}
Trigger Client events When you click Sign In

function Setpwdandchk () {
Take user name
var usr = document.getElementById (' txtUserName '). Value;
alert (USR);
Writes the last user information to a cookie
Setlastuser (USR);
If the Remember Password option is selected
if (document.getElementById (' chkrememberpwd '). Checked = = True) {
Take password value
var pwd = document.getElementById (' Txtpassword '). Value;
Alert (PWD);
var expdate = new Date ();
Expdate.settime (Expdate.gettime () + 14 * (24 * 60 * 60 * 1000));
Write a user name and password to a cookie
Setcookie (usr, pwd, expdate);
}
else {
Expire immediately if the remember password is not checked
Resetcookie ();
}
}

function Setlastuser (usr) {
var id = "49bac005-7d5b-4231-8cea-16939beacd67";
var expdate = new Date ();
Current time plus two-week time
Expdate.settime (Expdate.gettime () + 14 * (24 * 60 * 60 * 1000));
Setcookie (id, usr, expdate);
}
Call this method when the user name loses focus

function Getpwdandchk () {
var usr = document.getElementById (' txtUserName '). 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 = "";
}
}
Take the value of the cookie

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 example, 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>
<body>
<form id= "Form1" >
<div>
User name: <input 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"/>
</div>
</form>
</body>

Use JS to remember the user name and login password effect

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.