js實現自動登陸


/* *
 * 操作Cookie   添加   
 * @param name
 * @param value
 * @return
 
*/
function SetCookie(name,value,days) // 兩個參數,一個是cookie的名子,一個是值
{
     var Days = 30;
     if( typeof(days)=="undefined"||isNaN(days))
        Days=parseInt(days.toString());
      // 此 cookie 將被保存 30 天 -1為瀏覽器關閉  
     if(Days!=-1){
         var exp =  new Date();     // new Date("December 31, 9998");
        exp.setTime(exp.getTime() + Days*24*60*60*1000);
        document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
    } else{
        document.cookie = name + "="+ escape (value) + ";expires=-1";
    }
}

/* *
 * 操作Cookie 提取   后台必須是escape編碼
 * @param name
 * @return
 
*/
function getCookie(name) // 取cookies函數
{
     var arr = document.cookie.match( new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr !=  nullreturn unescape(arr[2]);  return  null;
}
/* *
 * 操作Cookie 刪除
 * @param name
 * @return
 
*/
function delCookie(name) // 刪除cookie
{   
     var exp =  new Date();
    exp.setTime(exp.getTime() - (86400 * 1000 * 1));
     var cval=getCookie(name);
     if(cval!= null)
        document.cookie = name + "="+ escape (cval) + ";expires="+exp.toGMTString();
}

/* *
 * 根據ID獲取對象
 * @param objName
 * @return
 
*/
function GetObj(objName){
     if( typeof(objName)=="undefined")
         return  null;
     if(document.getElementById)
         return eval('document.getElementById("'+objName+'")');
     else
         return eval('document.all.'+objName);
    
}
/* *
 * 給String 添加trim方法
 
*/
String.prototype.trim= function(){
     return  this.replace(/(^\s*)|(\s*$)/g, "");
 }
/* *
 * 給String添加isNullOrempty的方法
 
*/
String.prototype.isnullorempty= function(){
     if( this== null|| typeof( this)=="undefined"|| this.trim()=="")
         return  true;
     return  false;
    
}

 

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

< html >
   < head >
     < title >login.html </ title >
     < script  type ="text/javascript"  src ="jsTool.js" ></ script >
     < script  type ="text/javascript" >
             var cookieName_username="LOGIN_USER_NAME_TEST";
         var cookieName_password="LOGIN_PASSWORD_TEST";
         var cookieName_autologin="LOGIN_AUTO_TEST";
        
         // 得到Cookie信息
         function getUserInfoByCookie(){
             var uname=getCookie(cookieName_username);
             if(uname!= null&&!uname.toString().isnullorempty()){
                GetObj('ttuserName').value=uname;
                GetObj('ck_saveuser').checked= true;
            }
                
             var upass=getCookie(cookieName_password)
             if(upass!= null&&!upass.toString().isnullorempty()){
                GetObj('ttpassword').value=upass;
                GetObj('ck_savepass').checked= true;
            }
            
             var autologin=getCookie(cookieName_autologin)
             if(autologin!= null&&!autologin.toString().isnullorempty())
                 if(autologin.toString().trim()=="true"){
                    GetObj('ck_autologin').checked= true;
                    login(); // 登錄               
                }
        }
         /* *
        *登錄(保存信息)
        
*/
         function login(){
             var tusername=GetObj('ttuserName');
             if(tusername.value.trim().isnullorempty()){
                alert('您尚未輸入用戶名!請輸入!');
                tpassword.focus();
                 return;
            }
             var tpassword=GetObj('ttpassword');
             if(tpassword.value.trim().isnullorempty()){
                alert('您尚未輸入密碼!請輸入!');
                tpassword.focus();
                 return;
            }
            delCookie(cookieName_username); // 刪除用戶名Cookie
             var tsaveuser=GetObj('ck_saveuser');            
             if(tsaveuser.checked)        
                SetCookie(cookieName_username,tusername.value.trim(),7); // 保存到Cookie中  保存7天
            delCookie(cookieName_password); // 刪除密碼
             var tsavepass=GetObj('ck_savepass');
             if(tsavepass.checked)
                SetCookie(cookieName_password,tpassword.value.trim(),7);
             var autologin=GetObj('ck_autologin');        
            SetCookie(cookieName_autologin,autologin.checked,7);
            alert('登錄成功!');
        
        }
     </ script >
   </ head >  
   < body  onload ="getUserInfoByCookie()" >
        用戶名: < input  type ="text"  value =""  name ="ttuserName"  id ="ttuserName"  maxlength ="16" />
        密碼: < input  type ="password"  value =""  name ="ttpassword"  id ="ttpassword"  maxlength ="16" />
         < input  type ="checkbox"  name ="ck_saveuser"  id ="ck_saveuser" />保存用戶名
         < input  type ="checkbox"  name ="ck_savepass"  id ="ck_savepass" />保存密碼
         < input  type ="checkbox"  name ="ck_autologin"  id ="ck_autologin" />自動登錄
         < input  type ="button"  value ="登錄(保存)"  onclick ="login()" />
   </ body >
</ html >


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM