使用 cookie:
在登录页面:
<script type="text/javascript">
if(document.cookie.split(";")[0].split("=")[0] != "")//the login page checks for cookies every time it loads.if the password and id are saved,it'll redirects to homepage without showing login page
{
window.location.href = "welcome.html";
}
if(condition)//condition when username and password are valid
{
var uname=document.getElementById("zname").value;
var pswd=document.getElementById("zpswd").value;
if(document.getElementById("chkbx").checked) //Remember me checkbox (stores username and password until "logout" is clicked in homepage)
{
document.cookie=uname+"="+pswd;
alert("You will be automtically logged in when you visit this page again" +uname);
}
window.location.href="welcome.html";
}
</script>
在主页中:
<script type="text/javascript">
function del_cookie(name)
{
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
function signOut()
{
del_cookie(document.cookie.split(";")[0].split("=")[0]);
window.location.href ="login_page.html"
}
</script>
因此,登录页面每次加载时都会检查 cookie。如果密码和ID被保存,它将重定向到主页而不显示登录页面