jQuery保存cookie

/**
 * 登录
 */
var login = function(){
 
 var validate1 = false;
 var validate2 = false;
 var $userName=$('input[name="userName"]'),
  $password=$('input[name="password"]'),
  $jurisdiction,
  $rememberMe;
 
 function init(){
  if($userName.val()!="")
   validate1 = true;
  if($password.val()!="")
      validate2 = true;
      
     $userName.blur(function(){
      if($(this).val()=='')
       validate1 = false;
      else
       validate1 = true;
     })
     $password.blur(function(){
      if($(this).val()=='')
       validate2 = false;
      else
       validate2 = true;
     })
      
 
  var username = getCookieValue("user");  
        var password = getCookieValue("password");
  var jurisdiction = getCookieValue("jurisdiction");
                               
        if (username !='' && password !='' ) {  
              $userName.val(username);  
              $password.val(password);  
        $jurisdiction.val(jurisdiction)
              $rememberMe.attr("checked", true);  
        }else  
              $rememberMe.attr("checked", false);  
 }
 
 /* 登录 */
 function logining() {
  
  if (!validate1 || !validate2 ) {

   return false;
  }
  $jurisdiction = $('#jurisdiction input');
  
  // 记住账号
  $rememberMe = $('input[name="rememberMe"]');
  if($rememberMe.attr("checked") == 'checked')
  {
   setCookie('user', $userName.val(), 60 * 60 * 60, '/');
   //set 获取用户名和密码 传给cookie  
            setCookie('password', $password.val(), 60 * 60 * 60, '/'); 
            setCookie('jurisdiction', $jurisdiction.val(), 60 * 60 * 60, '/'); 
  }
  
//  alert($userName.val()+','+$password.val()+','+$jurisdiction.val()+'.');
  $.ajax({
   type : 'post',
   url : "loginServlet.do?command=login",
   data : {
    userName:$userName.val(),
    password:$password.val(),
    jurisdiction:$jurisdiction.val()
//    rememberMe:$rememberMe.val()
   },
   success : function(result) {
//    alert('result='+result)
    if(result != 0)
    { 
     swal({
       title: "登录成功!",
       text: "页面跳转中...",
       timer: 1000,
       showConfirmButton: false,
       type:"success"
     });
     setTimeout(function(){ redirect(result) },"2000");
    }
    else
    {
     swal({
       title: "登录失败!",
       text: "请检查您的账号信息.",
       timer: 1000,
       showConfirmButton: false,
       type:"info"
     });
    }
   },
   error : function() {
    swal({
      title: "服务器异常!",
      text: "请稍后登录.",
      timer: 1000,
      showConfirmButton: false,
      type:"error"
    });
   }
  });
 }
 
 // 登陆成功,重定向
 function redirect(result){
  if(result == 1)
   location.href="student.html";
  if(result == 2)
   location.href="teacher.html";
  if(result == 3)
   location.href="admin.html";
 }
 
 
 //hours为空字符串时,cookie的生存期至浏览器会话结束。
 //hours为数字0时,建立的是一个失效的cookie,
 //这个cookie会覆盖已经建立过的同名、同path的cookie(如果这个cookie存在)。   
 function setCookie(name,value,hours,path){   
     var name = escape(name);   
     var value = escape(value);   
     var expires = new Date();   
     expires.setTime(expires.getTime() + hours*3600000);   
     path = path == "" ? "" : ";path=" + path;   
     _expires = (typeof hours) == "string" ? "" : ";expires=" + expires.toUTCString();   
     document.cookie = name + "=" + value + _expires + path;   
 }   
 //cookie名获取值
 function getCookieValue(name){   
     var name = escape(name);   
     //读cookie属性,这将返回文档的所有cookie   
     var allcookies = document.cookie;          
     //查找名为name的cookie的开始位置   
     name += "=";   
     var pos = allcookies.indexOf(name);       
     //如果找到了具有该名字的cookie,那么提取并使用它的值   
     if (pos != -1){    //如果pos值为-1则说明搜索"version="失败   
         var start = pos + name.length;   //cookie值开始的位置   
         var end = allcookies.indexOf(";",start); //从cookie值开始的位置起搜索第一个";"的位置,即cookie值结尾的位置   
         if (end == -1) end = allcookies.length; //如果end值为-1说明cookie列表里只有一个cookie   
         var value = allcookies.substring(start,end);  //提取cookie的值   
         return unescape(value);       //对它解码         
         }      
     else return "";    //搜索失败,返回空字符串   
 }  
 
 return {
  init:function(){
   init();
  },
  logining:function(){
   logining();
  }
 }
}()

login.init(); // js文件在body下面引入就可以了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值