标签:use javascrip ext java string expires ring null rem
<html>
<head>
<title></title>
</head>
<meta charset="utf-8">
<body>
用户名<input type="text" name="userName" id="userName" value="" /><br /> 密码
<input type="password" name="userPass" id="userPass" value="" /><br />
<input type="checkbox" name="" id="rem" value="" />记住用户名 <br />
<button id="log">登录</button>
</body>
//注意 ---引用自己的jq
<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script>
///设置cookie
function setCookie(NameOfCookie, value, expiredays) {
var ExpireDate = new Date();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}
///获取cookie值
function getCookie(NameOfCookie) {
if(document.cookie.length > 0) {
begin = document.cookie.indexOf(NameOfCookie + "=");
if(begin != -1) {
begin += NameOfCookie.length + 1;
end = document.cookie.indexOf(";", begin);
if(end == -1){
end = document.cookie.length;
}
return unescape(document.cookie.substring(begin, end));
}
}
return null;
}
///删除cookie
function delCookie(NameOfCookie) {
if(getCookie(NameOfCookie)) {
document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
$(function() {
//加载页面的时候
var userPass = getCookie("pass");
var userName = getCookie("user");
var rember=getCookie("rember");
if(rember=="true") {
$("#userName").val(userName);
$("#userPass").val(userPass);
$("#rem").prop("checked",true);
}
//点击登录的时候
$("#log").click(function() {
var status = $("#rem").prop("checked");
var user = $("#userName").val();
var pass = $("#userPass").val();
setCookie("rember", status);
if(status) {
setCookie("user", user);
setCookie("pass", pass);
}else{
delCookie("user");
delCookie("pass");
};
});
});
</script>
</html>
标签:use javascrip ext java string expires ring null rem
原文地址:http://www.cnblogs.com/tingyatou/p/6905108.html