[英]Direct a HTML page to login page
我已經創建了兩個HTML頁面,一個頁面名為login.html
,另一個頁面名為resume.html
。
我想做的是,無論我打開哪個頁面,我都將始終重定向到登錄頁面。 如果我打開login.html
,它將被打開,並填寫username
和password
字段,則resume.html
將打開。
我的問題是,當我打開resume.html
時,我不知道應添加到重定向到login.html
的resume.html
。
登錄頁面的代碼為:
<html>
<head>
<title>
Login page
</title>
</head>
<body>
<h1 style="font-family:Comic Sans Ms;text-align:center;font-size:50pt;color:#ff00ba;">
You need to login :) </h1>
<form style="font-family:Comic Sans Ms; text-align:center">
Username<br>
<input type="text" name="userid"/><br>
Password<br>
<input type="password" name="pswrd"/>
<br><br>
<input style="font-family:Comic Sans Ms;" type="button" onclick="check(form)" value="Login">
<input style="font-family:Comic Sans Ms;" type="reset" value="Cancel"/>
</form>
<script language="javascript">
function check(form)
{
if(form.userid.value == "pwan9047" && form.pswrd.value == "Wang1984")
{
window.open('resume.html')
}
else
{
alert("Error Password or Username")
}
}
</script>
</body>
</html>
單擊該按鈕后,可以在localStorage
注冊用戶是否已登錄。
所以改變這個:
if(form.userid.value == "pwan9047" && form.pswrd.value == "Wang1984")
{
window.open('resume.html')
}
對此:
if(form.userid.value == "pwan9047" && form.pswrd.value == "Wang1984")
{
localStorage.setItem('isLoggedIn', true);location.href='/resume.html';
}
當然,在login.html
中將注銷按鈕添加到您的html中,以便能夠注銷:
<input style="font-family:Comic Sans Ms;" type="button" value="Log out" onclick="localStorage.removeItem('isLoggedIn');location.href='/login.html'"/>
並將此代碼添加到/resume.html
的<head>
和僅在登錄后才希望訪問的任何其他頁面:
<script>
window.onload=function(){
if(localStorage.getItem('isLoggedIn')==null){
location.href="/login.html";
}
}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.