外部 JavaScript 未运行,不写入文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26821566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:32:26  来源:igfitidea点击:

External JavaScript Not Running, does not write to document

javascripthtmlexternal

提问by Jon Q

I'm trying to use an external JavaScript file in order to write "Hello World" into a HTML page.

我正在尝试使用外部 JavaScript 文件将“Hello World”写入 HTML 页面。

However for some reason it does not work, I tried the same function and commands inline and it worked, but not when it's using an external JavaScript file. The part I commented out in the JS file was the previous method I was trying to use. Those lines of could worked when I ran the script from the header, and inline. Thanks

但是由于某种原因它不起作用,我尝试了相同的函数和内联命令并且它起作用了,但在使用外部 JavaScript 文件时不起作用。我在 JS 文件中注释掉的部分是我之前尝试使用的方法。当我从标题和内联运行脚本时,这些行可以工作。谢谢

Html file:

html文件:

<html>

    <head>
    </head>

<body>

    <p id="external">

        <script type="text/javascript" src="hello.js">
            externalFunction();
        </script>
    </p>

    <script type="txt/javascript" src="hello.js"></script>

</body>

</html>

JavaScript file

JavaScript 文件

function externalFunction() 
        {
         var t2 = document.getElementById("external");

            t2.innerHTML = "Hello World!!!"

         /*document.getElementById("external").innerHTML = 
         "Hello World!!!";*/

        }

采纳答案by Pedro Lopez

In general, you want to place your JavaScript at the bottom of the page because it will normally reduce the display time of your page. You can find libraries imported in the header sometimes, but either way you need to declare your functions before you use them.

通常,您希望将 JavaScript 放在页面底部,因为它通常会减少页面的显示时间。有时您可以在头文件中找到导入的库,但无论哪种方式,您都需要在使用它们之前声明您的函数。

http://www.w3schools.com/js/js_whereto.asp

http://www.w3schools.com/js/js_whereto.asp

index.html

索引.html

<!DOCTYPE html>
<html>

<head>
  <!-- You could put this here and it would still work -->
  <!-- But it is good practice to put it at the bottom -->
  <!--<script src="hello.js"></script>-->
</head>

<body>

  <p id="external">Hi</p>

  <!-- This first -->
  <script src="hello.js"></script>

  <!-- Then you can call it -->
  <script type="text/javascript">
    externalFunction();
  </script>

</body>

</html>

hello.js

你好.js

function externalFunction() {
  document.getElementById("external").innerHTML = "Hello World!!!";
}

Plunker here.

普朗克在这里

Hope this helps.

希望这可以帮助。

回答by rlemon

Script tags with SRC values do not run the contents. Split it to two script tags. One for the include, one for the function call. And make sure the include is before the call.

带有 SRC 值的脚本标签不运行内容。将其拆分为两个脚本标记。一种用于包含,一种用于函数调用。并确保包含在调用之前。

回答by ZeroBased_IX

You're trying to call the function before it has been loaded.

您试图在加载之前调用该函数。

Place the load script above the declaration:

将加载脚本放在声明上方:

<html>

    <head>
<script type="txt/javascript" src="hello.js"></script>
    </head>

<body>

    <p id="external">

        <script type="text/javascript">
            externalFunction();
        </script>
    </p>



</body>

</html>

Also you have a typo:

你还有一个错字:

 <script type="txt/javascript" src="hello.js"></script>

Should be:

应该:

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

The script type needs to be "text/javascript" not "txt/javascript".

脚本类型需要是“text/javascript”而不是“txt/javascript”。

回答by karabuta

use onload eventListener to make it simple

使用 onload eventListener 使其简单

<script>
   window.onload = function() {
      externalFunction();
   }
</script>