javascript - Direct a HTML page to login page -


i have created 2 html pages, 1 called login.html , other -- resume.html.

what want is, no matter page open, i'll redirected login page. if open login.html, opened , fill in username , password fields, resume.html opens.

my problem is, don't know code, should add, redirected login.html, when open resume.html.

the code of login page :

  <html>   <head>     <title>    login page    </title>    </head>    <body>    <h1 style="font-family:comic sans ms;text-align:center;font-size:50pt;color:#ff00ba;">    need 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> 

you can register in localstorage whether user logged in, when button clicked.

so change this:

if(form.userid.value == "pwan9047" && form.pswrd.value == "wang1984")   {     window.open('resume.html')   } 

to this:

if(form.userid.value == "pwan9047" && form.pswrd.value == "wang1984")   {     localstorage.setitem('isloggedin', true);location.href='/resume.html';   } 

add log out button html in login.html, able log out, of course:

<input style="font-family:comic sans ms;" type="button" value="log out" onclick="localstorage.removeitem('isloggedin');location.href='/login.html'"/> 

and add code in <head> of /resume.html , other page want accessible if logged in:

<script> window.onload=function(){     if(localstorage.getitem('isloggedin')==null){        location.href="/login.html";     } } </script> 

Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -