简体   繁体   English

Javascript文件无法加载

[英]Javascript file will not load

I have a simple script that works when included in the html body. 我有一个简单的脚本,当包含在html正文中时可以工作。 However, when running this from a relative path, and including the 但是,从相对路径运行时,包括

<script type="text/javascript" src="simple.js"></script>

in either the header or bottom of the html file, the script will no longer run. 在html文件的标题或底部,脚本将不再运行。

Can anyone point as to what I am missing ? 任何人都可以指出我错过了什么?

Thank you. 谢谢。

Script simple.js: 脚本simple.js:

<script>
        function firstFunction(){

            var a = document.getElementById("firstInput").value;

            if(! a){
                alert("minimum input of 1 char required");
            }else{
                document.getElementById("firstOutput").innerHTML=a;
            }


        }

        function secondFunction(){
            var b = document.getElementById("secondInput").value;

            if(!b){
                alert("minimum input of 1 char required");
            }


            document.getElementById("secondOutput").innerHTML=b;
            //document.getElementById("secondOutput").innerHTML = "updated!";
        }

You only need the <script> tag if you include the javascript into your html file. 如果将javascript包含在html文件中,则只需要<script>标记。

In a .js file, it's a syntax error. 在.js文件中,这是一个语法错误。 Just write your javascript code without the tag! 只需编写没有标记的javascript代码!

File simple.js: 文件simple.js:

function firstFunction(){

        var a = document.getElementById("firstInput").value;

        if(!a){
            alert("minimum input of 1 char required");
        }else{
            document.getElementById("firstOutput").innerHTML=a;
        }


    }

    function secondFunction(){
        var b = document.getElementById("secondInput").value;

        if(!b){
            alert("minimum input of 1 char required");
        }


        document.getElementById("secondOutput").innerHTML=b;
        //document.getElementById("secondOutput").innerHTML = "updated!";
    }

As your file is right now, make sure to place the script right before the closing body tag 由于您的文件现在是,请确保将脚本放在关闭正文标记之前

<script type="text/javascript" src="simple.js"></script>
</body>

so that the elements can be found when the script is running. 以便在脚本运行时可以找到元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM