【问题标题】:redirect user to another html page if the condition is true如果条件为真,则将用户重定向到另一个 html 页面
【发布时间】:2017-03-25 03:50:03
【问题描述】:

首先,我只想说我对html不好,我对html的了解都是我自己在网上学的。

所以.. 我有一个项目,其中一部分是建立一个网站。我已经建立了网站,但我有一个问题..

我的问题是我不想让用户访问设置,我希望管理员只能访问设置,我通过在设置页面中输入密码来做到这一点......所以如果用户点击设置图标,它会要求输入密码,如果正确则用户可以进入设置页面

我有 3 个页面(index.html 这是我的主页)、(我的设置代码所在的 manager.html)和(此代码所在的 index4) 在 index.html 中有一个代码可以让用户转到 index4.html 页面 在 index4.html 页面中是这段代码

<!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>

根据我在网上学到的知识,href="link" 代码可以重定向到另一个页面.. 但如果它在“if 语句”中,我似乎无法使用它。 当我检查元素时,我收到此错误“意外令牌'

谁能告诉我如何纠正我的错误。

【问题讨论】:

  • 您不能在 javascript 块中使用 HTML .... location 是您需要检查的内容

标签: javascript html hyperlink href


【解决方案1】:

另一种方法是使用如下。检查这个link

例如:

// 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】:

    尝试设置document.location.href - 更多关于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");
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 2012-04-05
      • 1970-01-01
      • 2015-01-17
      • 1970-01-01
      • 2013-08-17
      相关资源
      最近更新 更多