redirect user to another html page if the condition is true

允我心安 提交于 2019-12-11 02:27:08

问题


First of all, I just want to say that I'm not good with html, all i know about html is from what i learn online by myself.

So.. I have a project and part of it is making a website. I already made the website but i have a problem..

My problem is I don't want to let the user access the setting, I want the admin to only access the setting, I did that by putting a password in setting page.. so if the user clicks on the setting icon, it will ask for a password, if its correct then the user can go to setting page

I have 3 pages (index.html which is my home page),(manager.html where my setting code is in) and (index4 where this code is in) in index.html there a code which let the user go to index4.html page and in index4.html page is this code

<!DOCTYPE html>

<html>

<body>

    <script>
        var pass;
        pass = prompt("please enter the password to access");
        if (pass == "11111"){
            <a href="manager.html">This is the link.</a>        
        } else {
            document.write("The password you've is enter is incorrect");
        }
</script>

<body>  

</html>

From what I learn online, href="link" code can redirect to another page.. but it seems I can't use it if it's in 'if statement'. when I did inspect element,I got this error "Unexpected token '<'"

Can someone please tell me how to correct my mistake.


回答1:


Another way is to use as bellow. Check this link

For example:

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";



回答2:


try setting document.location.href - more on that here

var pass;
pass = prompt("please enter the password to access");
if (pass == "11111") {
  document.location.href = "manager.html";
} else {
  document.write("The password you've is enter is incorrect");
}


来源:https://stackoverflow.com/questions/40539097/redirect-user-to-another-html-page-if-the-condition-is-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!