仅前端cookie之记住密码

参考文章给忘了。。。,我就在他基础上修改了一些,但至于安全性,我没弄md5,所以安全系数应该为0

<!DOCTYPE html>
<html lang="en">
<head>
</head>

<body class="no-skin" onload="checkCookie()">

<form action="" name="userForm" id="userForm" method="post">
   <table>
      <tr>
         <td><label for="username">用户名:</label></td>
         <td><input type="text" name="username" id="username" /></td>
      </tr>
      <tr>
         <td><label for="password">密码:</label></td>
         <td><input type="password" name="password" id="password" /></td>
      </tr>
         <td colspan="2"><input type="submit" value="注册" onclick="checkCookie()"/></td>
         <td><label for="remmber">记住密码</label></td>
         <td><input type="checkbox" value="flag" name="remmber" id="remmber"/></td>
      </tr>
   </table>
</form>
</body> 
<script type="text/javascript">
   $(top.hangge());

    <!-- 记住密码 -->
        function getCookie(c_name)      //根据分隔符每个变量的值
        {
            if (document.cookie.length > 0) {
                c_start = document.cookie.indexOf(c_name + "=")
                if (c_start != -1) {
                    c_start = c_start + c_name.length + 1;
                    c_end = document.cookie.indexOf("^",c_start);
                    if (c_end==-1)
                        c_end=document.cookie.length;
                    return unescape(document.cookie.substring(c_start,c_end));
                }
            }
            return "";
        }

    function setCookie(c_name, n_value, p_name, p_value, expiredays)        //设置cookie
    {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(n_value) + "^" + p_name + "=" + escape(p_value) + ((expiredays == null) ? "" : "^;expires=" + exdate.toGMTString());
        console.log(document.cookie)
    }

    function checkCookie()      //检测cookie是否存在,如果存在则直接读取,否则创建新的cookie
    {
        //alert(document.cookie)
        var username = getCookie('username');
        var password = getCookie('password');
        if (username != null && username != "" && password != null && password != "") {
            $("#username").val(username);
            $("#password").val(password);
        } else {
         if(document.getElementById('remmber').checked) {
                username = $("#username").val();
                password = $("#password").val();

                if (username != null && username != "" && password != null && password != "") {
                    setCookie('username', $.md5(username), 'password', $.md5(password), 365);
                }
            }
        }
        //alert(document.cookie)
    }

    function cleanCookie (c_name, p_name) {     //使cookie过期
        document.cookie = c_name + "=" + ";" + p_name + "=" + ";expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
</script>
</html>

  

原文地址:https://www.cnblogs.com/yuanmaolin/p/11076189.html