<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
body{background: #CCCCCC;}
.box{margin: 20px auto; width: 300px;font-size: 14px;}
.box input{margin-bottom: 20px; outline: none;}
</style>
</head>
<body>
<div class="box">
<input type="text" id="name" value="" /><br />
<input type="password" name="password" id="password" value="" /><br />
七天免登陆?<input type="checkbox" id="save" value="" />
<input type="button" id="login" value="login" />
</div>
<script type="text/javascript">
var _login = document.getElementById("login");
var _name = document.getElementById("name");
var _password = document.getElementById("password");
_password.onblur = function(){
if(/\w{6,20}/g.test(this.value)){
_login.onclick = function(){
if(_name.value.length>0 && _password.value.length>=6 ){
document.cookie = "user="+_name.value+";expires="+new Date(new Date().getTime()+7*24*3600*1000);
document.cookie = "password="+_password.value+";expires="+new Date(new Date().getTime()+7*24*3600*1000);
window.location = "cont.html?name="+_name.value+"&password="+_password.value;
}else{
alert('请重新输入用户名和密码');
}
}
}else{
alert("密码错误,请重新输入")
}
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
if(window.location.search.length>0){
var _body = document.getElementsByTagName('body')[0];
var _arr = window.location.search.replace("?","").split("&");
var _name = _arr[0].split("=")[1];
var _pwd = _arr[1].split("=")[1];
_body.innerHTML = "登陆成功,欢迎"+_name+"到此一游";
}else{
var _cook = document.cookie;
if(_cook.indexOf('user')>=0){
var _arr1 = _cook.split(';');
var _name = '';
for(var i=0;i<_arr1.length;i++){
if(_arr1[i].indexOf('user')>=0){
_name = _arr1[i].split('=')[1];
}
}
document.getElementsByTagName('body')[0].innerHTML = "登陆成功,欢迎"+_name+"到此一游"
}
}
</script>
</body>
</html>
这篇博客通过JavaScript展示了如何实现七天免登录的逻辑。当用户访问页面时,如果URL中携带了用户名和密码参数,或者用户的cookie中存在用户信息,页面将显示“登陆成功,欢迎XXX到此一游”的消息,从而实现免登录的效果。

1876

被折叠的 条评论
为什么被折叠?



