js保存用戶名

Java代碼 
<head>  
<title>js COOKIE 記住帳號或密碼</title>  
<script language="javascript" type="text/javascript">  
function checkInCorrect()      //判斷用戶名和密碼是否為空 

     if (document.getElementById('txtUserName').value=="") 
   { 
    alert('請輸入用戶名!') 
    document.getElementById('txtUserName').focus(); 
    return false 
   } 
   if (document.getElementById('txtPassword').value=="") 
   { 
    alert('請輸入密碼!') 
    document.getElementById('txtPassword').focus(); 
    return false 
   } 
   else 
   { 
    saveInfo(); 
    return true; 
   } 

 
 
saveInfo = function(){ 
try{ 
   var isSave = document.getElementById('chkRememberPwd').checked;   //保存按鍵是否選中 
   if (isSave) { 
    var usernm = document.getElementById('txtUserName').value; 
    var userpsw = document.getElementById('txtPassword').value; 
    if(usernm!="" && userpsw!=""){ 
     SetCookie(usernm,userpsw); 
    } 
   }else {   
    SetCookie("",""); 
   } 
}catch(e){ 
 


 
function SetCookie(usern,psw){ 
var Then = new Date()  
Then.setTime(Then.getTime() + 1866240000000)  
document.cookie ="username=" + usern + "%%"+psw+";expires="+ Then.toGMTString() ; 

 
 
function GetCookie(){  
var nmpsd; 
var nm; 
var psd; 
var cookieString = new String(document.cookie) 
var cookieHeader = "username=" 
var beginPosition = cookieString.indexOf(cookieHeader) 
cookieString = cookieString.substring(beginPosition); 
var ends=cookieString.indexOf(";"); 
if (ends!=-1){ 
   cookieString = cookieString.substring(0,ends); 

if (beginPosition>-1){ 
   nmpsd = cookieString.substring(cookieHeader.length); 
   if (nmpsd!=""){ 
    beginPosition = nmpsd.indexOf("%%"); 
    nm=nmpsd.substring(0,beginPosition); 
    psd=nmpsd.substring(beginPosition+2); 
    document.getElementById('txtUserName').value=nm; 
    document.getElementById('txtPassword').value=psd; 
    if(nm!="" && psd!=""){ 
     document.forms[0].checkbox.checked = true 
    } 
   }  


</script>  
</head>  
<body onLoad="document.getElementById('txtUserName').focus();GetCookie();"> 
<form> 
    用戶名:<input type="text" ID="txtUserName" onblur="GetPwdAndChk()"> <br> 
    密  碼:<input type="password" ID="txtPassword"> <br> 
    <input type="checkbox" ID="chkRememberPwd" />記住密碼 <br> 
    <input type="button" OnClick="checkInCorrect()" value="進入"/>  
</form>    
</body> 

You May Also Like