簡體   English   中英

將HTML頁面定向到登錄頁面

[英]Direct a HTML page to login page

我已經創建了兩個HTML頁面,一個頁面名為login.html ,另一個頁面名為resume.html

我想做的是,無論我打開哪個頁面,我都將始終重定向到登錄頁面。 如果我打開login.html ,它將被打開,並填寫usernamepassword字段,則resume.html將打開。

我的問題是,當我打開resume.html時,我不知道應添加到重定向到login.htmlresume.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM