js cookie實現(xiàn)記住密碼功能
一. js 實現(xiàn)記住密碼功能
html:
<form id="form22" name="form22" action="checklogin.action" method="post" > <div class="username" style="margin-top:50px;"> <label>用戶名:</label> <input type="text" name="username" id="userName" /> <span id="myuser" style="color: red; font-size:12px; font-weight: normal;"></span> </div> <div class="password"> <label>密 碼:</label> <input name="password" id="passWord" type="password" /> <span id="mypass" style="color: red; font-size:12px; font-weight: normal;"></span> </div> <div class="user_type" > <label> </label> <input type="checkbox" id="saveUserName" style="float: left; margin-top:3px;" /> <span> 記住用戶</span> </div> <input type="button" value="" class="btn_login" id="btn_login" onclick="checkform();"/> </form>
cookie.js:
function setCookie (name, value) { var Days = 30; //此 cookie 將被保存 30 天 var exp = new Date(); exp.setTime(exp.getTime() + 1000); if(value==""||value=="null" ||value=="null"||value==" "){ }else{ document.cookie = name + "="+ escape(value) +";expires=Sun, 17-Jan-2038 19:14:07 GMT"; } } function getCookie(sName){ var aCookie = document.cookie.split("; "); for (var i=0; i < aCookie.length; i++) { var aCrumb = aCookie[i].split("="); if (sName == aCrumb[0]) { return aCrumb[1]; } } return null; } function checkCookieExist(name){ if (getCookie(name)) return true; else return false; } function deleteCookie(name, path, domain){ var strCookie; // 檢查Cookie是否存在 if (checkCookieExist(name)){ // 設(shè)置Cookie的期限為己過期 strCookie = name + "="; strCookie += (path) ? "; path=" + path : ""; strCookie += (domain) ? "; domain=" + domain : ""; strCookie += "; expires=Thu, 01-Jan-70 00:00:01 GMT"; document.cookie = strCookie; } } function saveCookie(name, value, expires, path, domain, secure){ var strCookie = name + "=" + value; if (expires){ // 計算Cookie的期限, 參數(shù)為天數(shù) var curTime = new Date(); curTime.setTime(curTime.getTime() + expires*24*60*60*1000); strCookie += "; expires=" + curTime.toGMTString(); } // Cookie的路徑 strCookie += (path) ? "; path=" + path : ""; // Cookie的Domain strCookie += (domain) ? "; domain=" + domain : ""; // 是否需要保密傳送,為一個布爾值 strCookie += (secure) ? "; secure" : ""; document.cookie = strCookie; }
login.js
window.onload = function(){ //console.log("ctx: " + "${ctx}"); var name = getCookie("loginUserName"); document.getElementById("passWord").value=""; if(name != null && name != "") { document.getElementById("userName").value = name; document.getElementById("passWord").focus(); } else { document.getElementById("userName").focus(); } } function checkform(){ ...... var isChecked = document.getElementById("saveUserName").checked; if(isChecked) { setCookie("loginUserName",userName); } ...... }
二. jquery 實現(xiàn)記住密碼功能
參考:http://www.cnblogs.com/lindaZ/p/5069981.html
html:
<form class="form-signin"> <input type="text" id="username" name="account" autofocus required placeholder="用戶名" class="form-control" style="width: 250px; margin-bottom: 5px;"> <input type="password" id="password" name="password" required placeholder="密碼" class="form-control" style="width: 250px;"> <br/> <input id="remember_me" type="checkbox" name="remember_me" onkeydown="check_enter(event)" style="width:250;"> <span for="remember_me" onkeydown="check_enter(event)" style="width:250px">記住我</span> <br/><br/> <span class="btn btn-lg btn-primary btn-block">登 錄</span> </form> <script src="jquery.js" type="text/javascript"></script>
<script src="jquery.cookie.js" type="text/javascript"></script>
判斷checkbox是否被選中,若選中,則將存儲cookie:
if ($("#remember_me").attr("checked")) { $.cookie("rmbUser", "true", { expires: 7 }); //存儲一個帶7天期限的cookie $.cookie("username", account, { expires: 7 }); $.cookie("password", password, { expires: 7 }); } else { $.cookie("rmbUser", "false", { expire: -1 }); $.cookie("username", "", { expires: -1 }); $.cookie("password", "", { expires: -1 }); }
在每次刷新登錄頁面加載js時,取出cookie中的用戶名和密碼,若cookie不為空,用戶名和密碼輸入框被cookie里面的內(nèi)容填充,復(fù)選框設(shè)為勾上狀態(tài):
$().ready(function(){ //獲取cookie的值 var username = $.cookie('username'); var password = $.cookie('password'); //將獲取的值填充入輸入框中 $('#username').val(username); $('#password').val(password); if(username != null && username != '' && password != null && password != ''){ //選中保存秘密的復(fù)選框 $("#remember_me").attr('checked',true); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js實現(xiàn)內(nèi)容顯示并使用json傳輸數(shù)據(jù)
這篇文章主要為大家詳細介紹了js實現(xiàn)內(nèi)容顯示并使用json傳輸數(shù)據(jù)的方法,感興趣的小伙伴們可以參考一下2016-03-03使用Javascript判斷瀏覽器終端設(shè)備(PC、IOS(iphone)、Android)
WEB開發(fā)中如何通過Javascript來判斷終端為PC、IOS(iphone)、Android呢?可以通過判斷瀏覽器的userAgent,用正則來判斷手機是否是ios和Android客戶端,下面通過本文學(xué)習下吧2017-01-01