zoukankan      html  css  js  c++  java
  • 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="登录"/>
        </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>
    keep learning
  • 相关阅读:
    2019.8.15刷题统计
    2019.8.12刷题统计
    2019.8.11刷题统计
    2019.8.10刷题统计
    2019.8.9刷题统计
    2019.8.8刷题统计
    2019.8.7刷题统计
    2019.8.6刷题统计
    xuezhan.org 6.28
    xuezhan.org 6.27
  • 原文地址:https://www.cnblogs.com/gao-xiong/p/5879657.html
Copyright © 2011-2022 走看看