Javascript 从 HTML 链接时文件不工作

Javascript file is not working when linked from HTML

我是 javascript 的新手,我正在努力让它发挥作用。当我从 HTML link 到我的外部 .js 文件时,它不起作用,但是当我直接在 HTML 文件中输入脚本时,它起作用。我确定我在 HTML 中提供的 src link 是正确的。我已经检查过多次了。我的 linked .js 文件与 HTML 文件位于同一文件夹中。

这是 index.html 文件:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <title>Radar chart</title>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script src="RadarChart.js"></script>
    <style>
        body {
          overflow: hidden;
          margin: 0;
          font-size: 14px;
          font-family: "Helvetica Neue", Helvetica;
        }

        #chart {
          position: absolute;
          top: 50px;
          left: 100px;
        }   
    </style>
  </head>
  <body>
    <div id="body">
      <div id="chart"></div>
    </div>

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

  </body>
</html>

我得到的错误如下:

   file:///home/******/*****/Tempelates/Test/script.js Failed to load resource: net::ERR_FILE_NOT_FOUND

我正在使用 Linux。 我已经看过以下问题,但他们帮不了我。

Javascript files not working when linked, but work internally?

javascript file not working when linked from HTML

Javascript not working with HTML external link

我一直为此伤脑筋。帮助将不胜感激。谢谢。

编辑 该代码在 Windows 中运行良好,但在 linux 中运行不正常。我现在更糊涂了

您在使用 Google Chrome 吗? 我用这里的解决方案解决了我的问题:
Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found

I think you have already solved this by now, but for what matters I've had the same problem on Google chrome and I did the following to fix it:

  1. Choose Customize and Control Google Chrome (the button in the up, right corner)
  2. Choose Settings
  3. Extensions
  4. Unmark all the extensions there. (they should show as Enable instead of Enabled) Regards,

由于您使用的是 Linux,请确保您对正在使用的所有文件和文件夹具有 read/write 权限。这可能会导致这些类型的问题。

您想检查 script.js 是否与 html 位于同一文件夹中,如果不在同一文件夹中,则您需要使用路径到达该文件夹,然后引用 javascript 文件.

答案是OP。这是一个极其愚蠢的错误,我错误地存储了 "script.js" 的名称。我将其存储为 "srcipt.js"。我在 windows 中制作了单独的文件并在那里复制了代码,而不是复制文件,这就是它在那里工作的原因。