为什么我的 javascript 文件无法加载?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5791435/
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-08-23 18:52:19  来源:igfitidea点击:

why would my javascript file not be loading?

javascript

提问by Mild Fuzz

I am trying to load a JS file into my website, I have placed the file into exactly the same folder as one that does load, but firebug does not show that it has loaded, and it's effects are not visible.

我正在尝试将一个 JS 文件加载到我的网站中,我将该文件放入与加载的文件夹完全相同的文件夹中,但是 firebug 没有显示它已加载,并且它的效果不可见。

Why could this be?

为什么会这样?

Here is the code to load the script

这是加载脚本的代码

<script src="<?php bloginfo('template_url'); ?>/page-customs/js/QES_form_edits.js" type="text/javascript"></script>
<script src="<?php bloginfo('template_url'); ?>/page-customs/js/datepicker.js" type="text/javascript" charset="utf-8"></script>

The first is not being loaded, the second is.

第一个没有被加载,第二个是。

Here is the script itself:

这是脚本本身:

jQuery(document).ready(function($) {

$("fieldset#keyword p.instruction").html("Search for a name or description of an activity, venue (e.g. Link Center) 
    or other keywords (e .g. coach)");


});

Jquery is loading fine.

Jquery 加载正常。

采纳答案by Mild Fuzz

Firebug will not show a javascript file as loaded if the file will not run due to invalidation. Fix JS, fix problem.

如果文件因失效而无法运行,Firebug 将不会显示已加载的 javascript 文件。修复JS,修复问题。

回答by Chris Baker

Ensure that the file actually exists. If you "view source", from the browser, you can hilite then right-click and copy the URL of the script. Go back to the browser, open a new tab, and right-click->paste the URL into the address bar, press enter. Does the script load in the browser window? If not, check the capitalization and spelling.

确保该文件确实存在。如果您“查看源代码”,从浏览器中,您可以 hilite 然后右键单击并复制脚本的 URL。返回浏览器,打开一个新标签页,右键单击->将 URL 粘贴到地址栏中,按回车键。脚本是否加载到浏览器窗口中?如果不是,请检查大小写和拼写。

You script tag looks correct, so check your underlying assumptions.

您的脚本标签看起来是正确的,因此请检查您的基本假设。

EDIT

编辑

OK, since the file exists, let's determine why it isn't loading. You mentioned having Firebug, open that and switch to the 'Net' panel. You should see a GET for the script in question, along with its status. Is the status 200? Click that row and check the headers, as well as the Response tab within the expanded row. What do you see there?

好的,既然文件存在,让我们确定为什么它没有加载。你提到有 Firebug,打开它并切换到“网络”面板。您应该会看到相关脚本的 GET 及其状态。状态是200吗?单击该行并检查标题以及展开行中的“响应”选项卡。你在那里看到什么?

Can you provide an example URL where this is occurring?

你能提供一个发生这种情况的示例 URL 吗?

回答by El Guapo

The problem could be anything from file permissions or a poorly formatted file. I would check either of those. Also, is there anything in the "Net" tab in firebug that looks like it's at least trying to download the file?

问题可能来自文件权限或格式错误的文件。我会检查其中任何一个。另外,firebug 的“Net”选项卡中是否有任何内容看起来至少是在尝试下载文件?

回答by sdm350

You need to make sure you include

你需要确保你包括

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

in your source

在你的来源

回答by Raghavendra N

An invalid html can also cause js files not to load. In my case I had this html inside a <div>in the main <body>tag:-

无效的 html 也会导致 js 文件无法加载。在我的情况下,我<div>在主<body>标签的a中有这个 html :-

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <style type="text/css" style="display:none;">

All the content after this piece of code was not recognized properly by the browser.

这段代码之后的所有内容都没有被浏览器正确识别。