【问题标题】:script.js file not linking to to html files with script tag src attributescript.js 文件未链接到具有脚本标记 src 属性的 html 文件
【发布时间】:2016-01-14 08:25:38
【问题描述】:

我是 JavaScript 新手。当我尝试以下代码时,我的 js 文件没有被链接,并且 html 页面中的 JavaScript 代码没有被执行: html 和 JavaScript 文件都在同一路径。

我的html:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;>
<title>JS Code</title>
</head>
<body>
    HTML
    <script type="text/javascript" src="script.js">
    debugger;
    document.write("HI i am from HTML with js code")
    nowCall1();
    function nowCall1(){
        document.write("-Js.")
        //peakOil(changeCivilisationCallback); 
    }
    </script>
</body>
</html>

script.js:

function peakOil(callback) {
    document.write("HI i am from Js");
    callback();  // the parentheses mean the function is executed!

}

function changeCivilisationCallback(){
     document.write("HI i am from changeCivilisationCallback");
}

function nowCall(){
    document.write("Js.")
    //peakOil(changeCivilisationCallback); 
}

但是当我尝试删除 scr 属性时,将所有代码放入 html 脚本中。为什么当我尝试将代码放入 js 文件时,它没有被执行。我在 google chrome 浏览器中调试时遇到问题。

请帮助我理解javas cript,在此先感谢。

【问题讨论】:

  • 代码充满了错误。你想做什么?

标签: javascript html javascript-debugger


【解决方案1】:

如果脚本标签上存在 src 属性,则脚本标签的内容将被忽略,因此您需要在单独的脚本标签上添加外部脚本。

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

【讨论】:

  • 是的,你是对的,我尝试过同样的方法,但是,虽然我的 js 和 html 文件位于同一位置,但现在浏览器给出错误消息:“无法加载资源:net::ERR_FILE_NOT_FOUND” js 文件。由于这个原因,我的函数调用正在下降,因为它存在于未加载的 js 文件中。我是否需要遵循任何目录结构和路径才能将脚本文件加载到 html 文件中?
  • 如果script.js文件和html文件在同一个位置应该没问题,you can read a bit about html paths here
  • 我不知道为什么我仍然收到这个i.stack.imgur.com/53oWY.jpg,我附上了我的错误图片,现在我的 js 文件正在加载,但它说方法未定义。
  • 如果您按住 ctrl(或 cmd,如果是 mac)并单击 src attr 中的文件名,您的编辑器是否会打开 scripts.js 文件? - 如果不是,编辑器找不到文件
  • 不,它不会打开,我在 Windows 系统中使用谷歌浏览器。
【解决方案2】:

您应该使用脚本标签链接到外部脚本文件,或者在标签内编写代码,您不能同时使用这两种方法(当然您可以拥有多个脚本标签)。

您可以在w3schools 上找到关于此的说明:

如果存在“src”属性,&lt;script&gt; 元素必须为空。

【讨论】:

    【解决方案3】:

    这是更正后的 JavaScript。

    来自 HTML 的 JS 代码:

    document.write("HI i am from HTML with js code");
    nowCall1();
    
    function nowCall1() {
        document.write("-Js.");
            //peakOil(changeCivilisationCallback); 
    }
    

    script.js:

    function peakOil() {
        document.write("HI i am from Js");
    }
    peakOil();
    
    function changeCivilisationCallback() {
        document.write("HI i am from changeCivilisationCallback");
    }
    
    function nowCall() {;
        document.write("Js.")
            //peakOil(changeCivilisationCallback); 
    }
    

    在 HTML 中,您应该以这种方式添加脚本:

    <script type="text/javascript">
        //the rest of the code
    </script>
    

    【讨论】:

      【解决方案4】:

      我在 content="text/html 之后错过了 " 在元标记上

      有了这个修正,它工作正常,

      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html">
      <title>JS Code</title>
      </head>
      <body>
          HTML
          <script type="text/javascript" src="script.js"></script>
          <script type="text/javascript">
          debugger;
          document.write("HI i am from HTML with js code");
          alert("test1"); 
          document.writeln("\ after alert");
          nowCall();
          alert("test2"); 
          </script>
          </body>
      </html>
      

      Keja,感谢您的意见。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-18
        • 2016-10-10
        • 2018-08-12
        • 1970-01-01
        • 2014-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多