欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js cookie实现记住密码功能

程序员文章站 2022-06-20 16:39:01
 本文为大家分享了js和jquery记住密码功能的方法,具体如下 一. js 实现记住密码功能 html:

一. js 实现记住密码功能

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)){
  // 设置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的期限, 参数为天数
  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 实现记住密码功能

参考: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里面的内容填充,复选框设为勾上状态:

$().ready(function(){
    //获取cookie的值
    var username = $.cookie('username');
    var password = $.cookie('password');

    //将获取的值填充入输入框中
    $('#username').val(username);
    $('#password').val(password); 
    if(username != null && username != '' && password != null && password != ''){
  //选中保存秘密的复选框
     $("#remember_me").attr('checked',true);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。