繁体   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