Cookie: saves the user name and password, and cookie username and account

Source: Internet
Author: User

Cookie: saves the user name and password, and cookie username and account

CookiesRemember. js

/**
*
* Author: dzq
* Remember the account and password
*/
// Obtain the last logged-on user
Function GetLastUser (){
Var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67"; // GUID
Var name = GetCookie (id); // obtain the Cookie value
If (name! = Null ){
Document. getElementById ('user _ name'). value = name;
} Else {
Document. getElementById ('user _ name'). value = "";
}
GetPwdAndChk (); // check whether the corresponding password has a cookie
}
// Call this method when the user name loses focus to check whether the corresponding password has a cookie
Function GetPwdAndChk (){
Var name = $ ("# user_name"). val ();
Var password = GetCookie (name); // obtain the Cookie value
If (password! = Null ){
Document. getElementById ('checkboxlogin'). checked = true;
Document. getElementById ('user _ password'). value = password;
} Else {
Document. getElementById ('checkboxlogin'). checked = false;
Document. getElementById ('user _ password'). value = "";
}
}
// Obtain the 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); // obtain the cookie value.
I = document. cookie. indexOf ("", I) + 1;
If (I = 0)
Break;
}
Return null;
}
// Obtain the cookie value
Function getCookieVal (offset ){
Var endstr = document. cookie. indexOf (";", offset );
If (endstr =-1)
Endstr = document. cookie. length;
Return unescape (document. cookie. substring (offset, endstr ));
}
// Save the username of the Last Logon user
Function SetLastUser (name ){
Var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67 ";
Var expdate = new Date ();
// The current time plus two weeks
Expdate. setTime (expdate. getTime () + 14*(24*60*60*1000 ));
SetCookie (id, name, expdate); // write to Cookie
}
// Write to Cookie
Function SetCookie (name, value, expires ){
Var argv = SetCookie. arguments;
// Length = 3 in this example
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 ":"");
}
// If the password is not selected and the current user exists in the cookie, the cookie can also be cleared.
Function ResetCookie (){
Alert ("do not select Remember password ");
Var name = $ ("# user_name"). val ();
Var expdate = new Date ();
SetCookie (name, null, expdate); // write to Cookie
}

CookiesRemember. js ---- end


User-login.jsp

<Script type = text/javascript>

$ (Function (){
GetLastUser ();
});

// Log on
Function login (){
Var name = $ ("# user_name"). val ();
Var password = $ ("# user_password"). val ();
Var newUrl = "$ {url }";
If (name = "" | password = ""){
Window. wxc. xcConfirm ('user name and password cannot be blank ', too many wxc.xcconfirm.typeenum.info );
Return;
}

Var params = {
"Name": name,
"Password": password
};
// Write the last user information to the Cookie
SetLastUser (name );
If (! $ ("# CheckboxLogin"). attr ("checked") = false ){
Alert ("you have chosen to remember the password. We will remember your password:" + password );
Var expdate = new Date ();
Expdate. setTime (expdate. getTime () + 14*(24*60*60*1000 ));
// Write the user name and password to the Cookie
SetCookie (name, password, expdate );
}
Else {
// If the password is not selected, it will expire immediately
ResetCookie ();
}

$. Ajax ({
Type: "POST ",
Timeout: 20000,
Url: '<% = request. getContextPath () %>/user/passport/login ',
Data: params,
DataType: "json ",
Success: function (data ){
If (data. returnCode = 0 ){
If (newUrl! = Null & newUrl! = ""){
Window. location. href = "<% = request. getContextPath () %>" + "/" + newUrl;
} Else {
Window. location. href = "<% = request. getContextPath () %>/user/zonglan ";
}
} Else {
Window. wxc. xcConfirm (data. returnMsg, window. wxc. xcConfirm. typeEnum. error );
}
},
Error: function (XMLHttpRequest, textStatus, errorThrown ){
If (textStatus = 'timeout '){
Window. wxc. xcConfirm ('request timeout', window. wxc. xcConfirm. typeEnum. error );
} Else {
Window. wxc. xcConfirm ('Connection exception', window. wxc. xcConfirm. typeEnum. error );
}
},
Complete: function (xhr, ts ){

}
});
}

</Script>

<Input type = "text" placeholder = "Enter your mobile phone number/Email Address" class = "login_int" id = "user_name" onblur = "GetPwdAndChk ()">

<Input type = "password" placeholder = "enter the password" class = "login_int" id = "user_password">

<A href = "javascript: void (0);" onclick = "login ();"> log on now </a>

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.