2015-07-21 73 views
-2

我想让此代码重定向到另一个页面,当人员输入hello时。我该怎么办? 如何在javascript中为我的html网站设置密码

<p>Click the button to demonstrate the prompt box.</p> 

<button onclick="myFunction()">Try it</button> 

<p id="demo"></p> 

<script> 
function myFunction() { 
    var person = prompt("Please enter your password"); 

    if (person == hello) { 
     window.open("http://www.w3schools.com"); 
    } 
    else { 
     confirm("Invalid Password")} 
} 
</script> 
</body> 
</html> 
+1

问题与标题完全无关。 – Scott

+1

使用javascript(或任何客户端脚本)来控制密码访问通常是一个非常糟糕的主意。您可能不太安全的唯一方法是直接在页面上打印密码供所有用户查看。 – nomistic

+0

这只是一个实验密码项目 –

回答

0

像其他人所说的,这实在是不安全的。但是对于这个项目的缘故,试试这个:

<p>Click the button to demonstrate the prompt box.</p> 

<button onclick="myFunction()">Try it</button> 

<script> 
function myFunction() { 
    var person = prompt("Please enter your password"); 

    if (person == 'hello') { 
     document.location.href = "http://www.w3schools.com"; 
    } 
    else { 
     confirm("Invalid Password") 
    } 
} 
</script> 
  1. 确保周围添加引号你好,因为它要比较的人变量的字符串。

  2. 而不是使用window.open(),只需将document.location.href属性更改为新的URL。这会将新的URL加载到当前窗口中。 Window.open()会打开一个名称所暗示的新浏览器窗口,但由于弹出窗口阻止程序,该窗口并不总是有效。

-1

只是试试这个

<button onClick="window.location='page_name.php';" value="click here" /> 
+0

这并不能真正回答这个问题吗? –

相关问题