js儲存使用者名稱

來源:互聯網
上載者:User

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> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.