JavaScript使用cookie實(shí)現(xiàn)記住賬號(hào)密碼功能
很多登錄功能上都有個(gè)“記住密碼”的功能,其實(shí)無(wú)非就是對(duì)cookie的讀取。
下面展示這個(gè)功能的代碼,原作者已無(wú)法考究。。。。
測(cè)試方法:直接輸入賬號(hào)密碼,提交后,刷新頁(yè)面,再輸入同樣的賬號(hào),就可以顯示
<!DOCTYPE HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js COOKIE 記住帳號(hào)或密碼</title>
<script type="text/javascript">
window.onload=function onLoginLoaded() {
if (isPostBack == "False") {
GetLastUser();
}
}
function GetLastUser() {
var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";//GUID標(biāo)識(shí)符
var usr = GetCookie(id);
if (usr != null) {
document.getElementById('txtUserName').value = usr;
} else {
document.getElementById('txtUserName').value = "001";
}
GetPwdAndChk();
}
//點(diǎn)擊登錄時(shí)觸發(fā)客戶端事件
function SetPwdAndChk() {
//取用戶名
var usr = document.getElementById('txtUserName').value;
alert(usr);
//將最后一個(gè)用戶信息寫入到Cookie
SetLastUser(usr);
//如果記住密碼選項(xiàng)被選中
if (document.getElementById('chkRememberPwd').checked == true) {
//取密碼值
var pwd = document.getElementById('txtPassword').value;
alert(pwd);
var expdate = new Date();
expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
//將用戶名和密碼寫入到Cookie
SetCookie(usr, pwd, expdate);
} else {
//如果沒(méi)有選中記住密碼,則立即過(guò)期
ResetCookie();
}
}
function SetLastUser(usr) {
var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";
var expdate = new Date();
//當(dāng)前時(shí)間加上兩周的時(shí)間
expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
SetCookie(id, usr, expdate);
}
//用戶名失去焦點(diǎn)時(shí)調(diào)用該方法
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 = "";
}
}
//取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));
}
//寫入到Cookie
function SetCookie(name, value, expires) {
var argv = SetCookie.arguments;
//本例中l(wèi)ength = 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> 用戶名:
<input type="text" ID="txtUserName" onblur="GetPwdAndChk()">
<input type="password" ID="txtPassword">
密碼:
<input type="checkbox" ID="chkRememberPwd" />
記住密碼
<input type="button" OnClick="SetPwdAndChk()" value="進(jìn)入"/>
</div>
</form>
</body>
</html>
相關(guān)文章
在IE8上JS實(shí)現(xiàn)combobox支持拼音檢索功能
這篇文章主要介紹了在IE8上JS實(shí)現(xiàn)combobox支持拼音檢索功能的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05
JavaScript 中如何實(shí)現(xiàn)大文件并行下載
本文將介紹如何利用 async-pool 這個(gè)庫(kù)提供的 asyncPool 函數(shù)來(lái)實(shí)現(xiàn)大文件的并行下載。2021-05-05
原生JavaScript再網(wǎng)頁(yè)實(shí)現(xiàn)文本轉(zhuǎn)語(yǔ)音功能
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)原生JavaScript再網(wǎng)頁(yè)實(shí)現(xiàn)文本轉(zhuǎn)語(yǔ)音功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2025-03-03
微信小程序如何獲知用戶運(yùn)行小程序的場(chǎng)景教程
這篇文章主要給大家介紹了在微信小程序中如何獲知用戶運(yùn)行小程序場(chǎng)景的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)跟著小編一起來(lái)看看吧。2017-05-05
JavaScript實(shí)現(xiàn)仿windows文件名稱排序
在JavaScript中,數(shù)組排序是一個(gè)常見的操作,本文將通過(guò)一個(gè)具體的代碼示例,解釋如何實(shí)現(xiàn)一個(gè)仿windows文件名稱的排序算法,需要的可以參考下2024-12-12
解決iframe的frameborder在chrome/ff/ie下的差異
最近的項(xiàng)目中使用了動(dòng)態(tài)創(chuàng)建iframe的js方法,發(fā)現(xiàn)iframe.frameborder="0"在IE7下不管用,而chrome/ff都正常的,很是郁悶。2010-08-08
JS?解決Cannot?set?properties?of?undefined的問(wèn)題
遇到這樣問(wèn)題當(dāng)前的是當(dāng)前對(duì)象或者數(shù)組是undefined,但是卻用來(lái)引用屬性或者索引,遇到這樣的問(wèn)題如何解決呢,下面通過(guò)本文給大家介紹JS?如何解決Cannot?set?properties?of?undefined,需要的朋友可以參考下2024-01-01

