Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么不是';我的html/javascript代码不能工作吗?_Javascript_Html_Concat - Fatal编程技术网

为什么不是';我的html/javascript代码不能工作吗?

为什么不是';我的html/javascript代码不能工作吗?,javascript,html,concat,Javascript,Html,Concat,我试图做到这一点,当我进入网站时,有一个按钮,当你按下它时,它会提示你“密码是什么?”以确定它是否正确,我使用concat字符串获取提示答案并将.html添加到结尾,我希望它重定向到(passwordtheyenter).html,因此如果他们输入“test”作为密码,它将把他们带到“test.html” 这是我的密码: <!DOCTYPE html> <html> <head> <script> function test

我试图做到这一点,当我进入网站时,有一个按钮,当你按下它时,它会提示你“密码是什么?”以确定它是否正确,我使用concat字符串获取提示答案并将.html添加到结尾,我希望它重定向到(passwordtheyenter).html,因此如果他们输入“test”作为密码,它将把他们带到“test.html”

这是我的密码:

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

函数testFunction(){
var str1=提示。(“密码是什么?”);
var str2=“.html”;
var pass=str1.concat(str2);
}
闲聊
打开。窗口(通行证)

谢谢。

pass变量仅在
testFunction
中定义,因此在尝试调用
open.window
时无法访问它


尝试在
testFunction
中返回
pass
,并将结果传递给
open。窗口

HTML/Javscript代码无法正常工作有四个主要原因

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

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

接下来,我相信您正在尝试使用window.open()函数。您在代码中编写了open.window()

第三,在prompt()调用之后意外出现了句点

第四,在函数中应该有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>

函数testFunction(){
var str1=提示(“密码是什么?”);
var str2=“.html”;
var pass=str1+str2;
窗口。打开(通行证);
}
闲聊

您可能正在寻找:

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>

打开。窗口(通行证)

它正在工作:

尝试用下面的脚本标记替换第一个脚本标记。 建议使用标记的“type”属性定义脚本类型

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

函数testFunction(){
var str1=提示(“密码是什么?”);
window.open(str1+“.html”);
}

您的代码存在以下问题:

  • 提示符。(
    是语法无效的Javascript代码。它会给您一个错误,可以在控制台中看到(您可以在大多数浏览器中按F12或在Opera中按CTRL+SHIFT+I来打开它)

  • open.window()
    是语义无效的Javascript代码。没有全局变量
    打开
    。您可能需要的是
    窗口。打开(…)

  • 代码的编写方式没有多大意义。单击按钮时,将调用函数,但它不执行任何操作。底部的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>
    
    
    闲聊
    //扣上按钮
    var myButton=document.getElementById(“我的按钮”);
    //在按钮上设置单击处理程序
    myButton.onclick=函数(){
    //获取页面
    var page=prompt(“密码是什么?”);
    //为窗口设置一个新位置;这将发送
    //将当前页面转换为新页面
    window.location=page+“.html”;
    };
    
    什么是
    提示。
    ?您应该打开控制台(在大多数浏览器中为F12)。它会让你知道你的代码中是否有错误。如果有人尝试了一些密码并发现了你不想让他们看到的页面,会发生什么情况?你永远不应该使用客户端验证作为身份验证的主要手段。我真的不知道如何在这个网站上回复,我注册是为了问这个问题@HoboSapiens我只准备这个页面和重定向页面我真的不知道如何在这个网站上回复,我注册是为了问这个问题。@MatthewRath这不是为了一些非常重要的事情,我不需要保存数据以便以后登录,但是如果你还有任何建议,我很乐意听到。对不起,我对这类东西真的不熟悉。你能详细说明一下怎么做吗?我真的很感激你的回答。你的open.window函数会在页面加载时执行,但你只会在用户单击按钮时提示用户。将open.window移到testFunction中,并删除按钮后面的脚本标记。(如果您是这样回答的),这是我的新代码,但仍然不起作用:在HTML5中,type属性是可选的,如果您有
    窗口,则默认为@draw的
    text/javascript
    。在
    testFunction
    中打开
    调用,则无需将
    pass
    变量设为全局变量