HTML頁面 使用Cookie記住密碼功能
阿新 • • 發佈:2019-01-27
$(function() {
getCookie();
});
// 回車鍵登陸
function keyLogin(){
if (event.keyCode==13){
document.getElementById("login").click();
}
};
// 使用者名稱密碼登陸
function login(){
if($("#loginName" ).val() == ""){
alert("請輸入使用者名稱");
return;
}
if($("#password").val() == ""){
alert("請輸入密碼");
return;
}
$.post("login!login.action",
{
"account.loginName" : $("#loginName" ).val(),
"account.passwd" : $("#password").val()
},function(data) {
saveInfo();
doLogin(data);
}, "json");
}
// 登陸後跳轉
function doLogin(data){
if (data != null && data.success) {
location.href = 'login!doIndex.action' ;
}
else {
if(data.message != null && data.message != ""){
alert(data.message);
doReset();
}else if(data.msg != null && data.msg != ""){
alert(data.msg);
doReset();
}
}
}
// 儲存Cookie
function saveInfo() {
try {
// 儲存按鍵是否選中
var isSave = document.getElementById('remember_password').checked;
if (isSave) {
var username = $("#loginName").val();
var password = $("#password").val();
if (username != "" && password != "") {
SetCookie(username, password);
}
} else {
SetCookie("", "");
}
} catch (e) {}
}
// 儲存Cookie
function SetCookie(username, password) {
var Then = new Date();
Then.setTime(Then.getTime() + 1866240000000);
document.cookie += ("username=" + username + "%%" + password + ";expires=" + Then.toGMTString());
}
// 獲取登陸的使用者名稱和密碼
function getCookie() {
var nmpsd;
var nm;
var psd;
var cookieString = new String(document.cookie);
var cookieHeader = "username=";
var beginPosition = cookieString.indexOf(cookieHeader);
cookieString = cookieString.substring(beginPosition);
var ends = cookieString.indexOf(";");
if (ends != -1) {
cookieString = cookieString.substring(0, ends);
}
if (beginPosition > -1) {
nmpsd = cookieString.substring(cookieHeader.length);
if (nmpsd != "") {
beginPosition = nmpsd.indexOf("%%");
nm = nmpsd.substring(0, beginPosition);
psd = nmpsd.substring(beginPosition + 2);
$("#loginName").val(nm)
$("#password").val(psd)
if (nm != "" && psd != "") {
document.getElementById('remember_password').checked = true;
}
}
}
}