【javascript】cookie的應用——記住使用者名稱

來源:互聯網
上載者:User

前面發表了一篇關於 cookie 的文章,封裝了 cookie.js,下面我們通過一個執行個體來應用這個 js。

最常見的就是記住使用者名稱,當使用者登入過一次後,通過 cookie 記錄下該使用者的帳號和密碼,這樣下次開啟頁面的時候不用再次輸入帳號密碼了。附上代碼:

<!DOCTYPE HTML><html lang="en-US"><head>    <meta charset="UTF-8">    <title>cookie的應用——記住使用者名稱</title></head><body>    <form action="#" id="myform">        <label for="username">使用者名稱:</label><input type="text" name="username" id="username" />        <label for="userpass">密碼:</label><input type="password" name="userpass" id="userpass" />        <input type="submit" value="登入" />        <a href="javascript:;">清除記錄</a>    </form></body></html><script type="text/javascript" src="cookie.js"></script><script type="text/javascript">window.onload = function(){    var oForm = document.getElementById('myform');    var oTxt1 = document.getElementById('username');    var oTxt2 = document.getElementById('userpass');    var oClear = document.getElementsByTagName('a')[0];    oTxt1.value = getCookie('username');    oTxt2.value = getCookie('userpass');        oForm.onsubmit = function(){        setCookie('username',oTxt1.value,30);        setCookie('userpass',oTxt2.value,30);    }    oClear.onclick = function(){        removeCookie('username');        removeCookie('userpass');        oTxt1.value = '';        oTxt2.value = '';    }}</script>

PS:這是本人閑著無聊,通過自己所學的 javascript 知識,隨意寫的一些效果。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.