2015-07-10 55 views
-1

我试图做到这一点,当我去到网站,有一个按钮,当你按下它,它会提示你“密码是什么?”为了确定它是否正确,我使用concat字符串来接收提示答案并添加.html到最后,并且我希望它重定向到(passwordtheyenter).html,因此如果他们输入“test”作为密码,它“会带他们去 “的test.html”为什么我的html/javascript代码不工作?

这里是我的代码:

<!DOCTYPE html> 
<html> 
<head> 
    <script> 
     function testFunction() { 
     var str1 = prompt.("What's the password?"); 
     var str2 = ".html"; 
     var pass = str1.concat(str2); 
     } 
    </script> 
</head> 
<body> 
<button onclick="testFunction()">Chat</button> 


<script> 
    open.window(pass) 
</script> 
</body> 
</html> 

感谢。

+1

什么是'提示()'?您应该打开控制台(大多数浏览器都是F12)。它会让你知道你的代码是否有错误。 –

+0

如果有人尝试使用几个密码并找到您不希望他们看到的页面,会发生什么情况? – 2015-07-10 01:48:17

+0

你永远不应该使用客户端验证作为你的主要认证手段。 –

回答

2

有你的HTML/Javscript代码不能正常工作的四个主要的原因。

首先,变量pass的范围仅在函数中定义,因此只能在该函数内进行访问。在函数之外定义它,然后在其中设置它的值。

编辑:因为pass现在只被函数使用,所以不需要在函数之外定义它。

接下来,我相信你试图使用window.open()函数。你在你的代码中写了open.window()。

第三,你有意外的提示经过一段时间的()调用

第四,你应该有window.open()函数中调用,以便它不运行,直到用实际点击按钮。

<!DOCTYPE html> 
<html> 
<head> 
    <script> 
     function testFunction() { 
      var str1 = prompt("What's the password?"); 
      var str2 = ".html"; 
      var pass = str1 + str2; 
      window.open(pass); 
     } 
    </script> 
</head> 
<body> 
<button onclick="testFunction()">Chat</button> 
</body> 
</html> 
+0

这对我来说,谢谢。 – Drew

+1

@Drew如果你在'testFunction'里面调用'window.open',那么你不需要在全局中设置'pass'变量 – jasonscript

0

pass变量只在testFunction内部定义,所以当您尝试呼叫open.window时,您无法访问它。

尝试在testFunction返回pass和结果传递给open.window

+0

对不起,我对这类东西真的很陌生。你能否详细说明如何做到这一点?我真的很感谢答案。 – Drew

+0

您的open.window函数在页面加载时得到执行,但您只在用户点击按钮时提示用户。将open.window移动到testFunction中,并删除按钮后面的脚本标记。 –

+0

(如果这是你的回复),这是我的新代码,但它仍然无法工作:http://pastebin.com/ZFUfre0W – Drew

0

你可能在寻找:

function testFunction() { 
    // Prompt user for input 
    var str1 = window.prompt("What's the password?"); 
    // Append .html to user input 
    var str2 = ".html"; 
    // Open new window with concatenated result 
    window.open(str1.concat(str2)); 
} 

然后,您可以删除:

<script> 
    open.window(pass) 
</script> 

这是工作:https://jsbin.com/gewudaruda/edit?html,output

0

尝试更换的第一个脚本标签与下面的一个。 建议使用标签的'type'属性来定义脚本的类型。是

<script type="text/javascript"> 
    function testFunction() { 
    var str1 = prompt("What's the password?"); 
    window.open(str1+".html"); 
    } 
</script> 
+0

在HTML5中,type属性是可选的,默认为'text/javascript'根据http://www.w3.org/TR/html5/scripting-1.html#attr-script-type – EasyCo

1

与您的代码中存在的问题如下:

  1. prompt.()在语法上是无效的JavaScript代码。它会给你一个错误,这可以在控制台中看到(你可以在大多数浏览器中按F12打开它,或者在Opera中按CTRL + SHIFT + I)。

  2. open.window()是语义无效的Javascript代码。这是没有全球变量open。你可能想要的是window.open(...)

  3. 您的代码写入方式没有多大意义。当按钮被点击时,该函数被调用,但它什么也不做。底部的Javascript会给你一个错误,但即使它是有效的Javascript,它仍然会给你一个错误,因为变量pass未在全局范围中定义;它是在函数内部定义的。一旦函数运行,变量将被遗忘。

这将是更好的按钮,而不是添加一个单击处理程序:

<!doctype html> 

<!-- The button --> 
<button id="my-button">Chat</button> 

<script> 
    // Get the button 
    var myButton = document.getElementById("my-button"); 

    // Set a click handler on the button 
    myButton.onclick = function() { 

     // Get the page 
     var page = prompt("What's the password?"); 

     // Set a new location for the window; this sends the 
     // current page to the new page 
     window.location = page + ".html"; 
    }; 
</script>