首页 > 代码库 > cookie应用(二):记住上一次用户名和密码

cookie应用(二):记住上一次用户名和密码

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Document</title></head><body>    <form class="form" action="http://www.baidu.com/">        用户名:<input type="text" name="username"/><br/>        密码:<input type="password" name="password"/><br/>        <input type="submit" value="http://www.mamicode.com/登录"/>    </form></body><script type="text/javascript">    window.onload = function(){        var oForm = document.getElementsByTagName(‘form‘)[0];        var oUsername = document.getElementsByName(‘username‘)[0];        var oPassword = document.getElementsByName(‘password‘)[0];        oForm.onsubmit = function(){            setCookie(‘username‘, oUsername.value, 14);            setCookie(‘password‘, oPassword.value, 14);        };        oUsername.value = getCookie(‘username‘);        oPassword.value = getCookie(‘password‘);    };    //设置cookie    function setCookie(name, value, iDay){        var oDate = new Date();        oDate.setDate( oDate.getDate() + iDay );        document.cookie = name+‘=‘+value+‘;expires=‘ + oDate;    };    //读取cookie    function getCookie(name){        var array1 = document.cookie.split("; ");        for(var i = 0; i < array1.length; i++ ){            var array2 = array1[i].split(‘=‘);            if(array2[0] == name){                return array2[1];            }        };        return ‘‘;    };    //删除cookie    function removeCookie(name){        setCookie(name, null, -1);    };</script></html>

 

cookie应用(二):记住上一次用户名和密码