I have created two HTML pages, one is called username.html and the other -- resume.html.
What I want to do is, that no matter which page I open, I'll be always redirected to login page. If I open the username.html, it will be opened up and I fill in the username and password fields, then the resume.html opens.
Here is my 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;">
You need to login :)
</h1>
<div style="text-align:center;">
<form style="font-family:Comic Sans Ms;">
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"/>
<input style="font-family:Comic Sans Ms;" type="button" value="Log out" onclick="localStorage.removeItem('isLoggedIn');location.href='/username.html'"/>
</form>
</div>
<script language="javascript">
function check(form){
if(form.userid.value == "pwan9047" && form.pswrd.value == "Wang1984"){
localStorage.setItem('isLoggedIn',true);location.href='/resume.html';
}else{
alert("Error Password or Username")
}
}
</script>
</body>
</html>
I also added the following JS code to the resume.html to let it direct to username.html when the resume.html is opened:
<script>
window.onload=function(){
if(localStorage.getItem('isLoggedIn')==null){
location.href="/username.html";
}
}
</script>
It works fine with my html editor (I am using Brackets as the editor), however, when I just click on the html files on my folder, there is a problem with it, the login page does not direct to resume page after I correctly fill the username and password, it shows page not found, the url is "file:///C:/resume.html", also when I click logout button, it has the same thing that page not found, the url is "file:///C:/username.html".
Thanks