HTML and Javascript code issue -
i have created 2 html pages, 1 called username.html , other -- resume.html.
what want is, no matter page open, i'll redirected login page. if open username.html, opened , fill in username , password fields, resume.html opens.
here 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> <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 added following js code resume.html let direct username.html when resume.html opened:
<script> window.onload=function(){ if(localstorage.getitem('isloggedin')==null){ location.href="/username.html"; } } </script>
it works fine html editor (i using brackets editor), however, when click on html files on folder, there problem it, login page not direct resume page after correctly fill username , password, shows page not found, url "file:///c:/resume.html", when click logout button, has same thing page not found, url "file:///c:/username.html".
thanks
the mistake here:
location.href="/username.html";
you started href forward slash ( / ) means url relative website root. in brackets, site runs in localhost server, http://localhost:xxxxx. root url indeed folder website, when open via normal filesystem (file:///), root c drive (file:///c:/) file isn't directly in c drive? or it? if plan publish site webserver, shouldn't worry, because works same in brackets, , go site root (like /public_html/
)
if want fix opening via normal filesystem, should use relative href file, not relative 1 root. remove forward slash this.
location.href="username.html";
now, start navigating folder current file is, if want navigate website/index.html
website/subfolder/file2.html
, can using
href="subfolder/file2.html
note navigating website/subfolder/file2.html
website/index.html
won't work anymore, because there no folder called 'website' inside 'subfolder' need navigate down, this:
href="../file2.html"
../
navigates down one folder, if want navigate down 2 folders (for example subfolder inside subfolder in root) can use 2 of them
href="../../file2.html"
Comments
Post a Comment