參考文章給忘了。。。,我就在他基礎上修改了一些,但至於安全性,我沒弄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>
