javascript 为什么我的 JS 文件加载不出来?

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

Why won't my JS file load?

javascripthtml

提问by Mild Fuzz

using this code:

使用此代码:

<script src="<?php bloginfo('template_url'); ?>/scripts/hovermenus.js" type="text/javascript"></script>

to loadthis script

加载这个脚本

firebug is not showing this script as loaded, however, these scripts:

firebug 未显示此脚本已加载,但是,这些脚本:

<script src="<?php bloginfo('template_url'); ?>/scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
    <script src="<?php bloginfo('template_url'); ?>/scripts/salf_ui.js" type="text/javascript"></script>
    <script src="<?php bloginfo('template_url'); ?>/scripts/date.js" type="text/javascript"></script>
    <script src="<?php bloginfo('template_url'); ?>/scripts/datePicker.js" type="text/javascript"></script>

all load perfectly well. I have double, triple and quadruple checked the file is in the right directory.

所有负载都很好。我已经双重、三重和四重检查文件是否在正确的目录中。

回答by Peter Ajtai

Looks like the problem is with your file

看起来问题出在您的文件上

Make sure the file .../scripts/hovermenus.jsexists. Navigate there and copy paste the file name from there.

确保文件.../scripts/hovermenus.js存在。在那里导航并从那里复制粘贴文件名。

Could the script be in another directory? could it be hoverMenus.jsor hover.menus.js.... etc.

脚本可以在另一个目录中吗?可能是hoverMenus.jshover.menus.js......等。

If none of this works, try copying and renaming the file. See if you can load the renamed version. If you can't, it's something in the file.

如果这些都不起作用,请尝试复制并重命名文件。看看你是否可以加载重命名的版本。如果你不能,它是文件中的东西。

Unrelated: You should declare your variables with varso you don't create a bunch of global variables attached to window.

不相关:您应该声明您的变量,var这样您就不会创建一堆附加到window.

回答by Peter Ajtai

I had the similar problem: same file would not load on other pages(master pages)

我有类似的问题:相同的文件不会加载到其他页面(母版页)

original: 

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

fixed:    
   <script type="text/javascript" src="/Scripts/scriptMob.js"></script>

only removed dots and first slash

只删除了点和第一个斜线

Additionally you can check in firebug(HTML section), expand the reference line

此外,您可以检查萤火虫(HTML 部分),展开参考线

    (+) <script src="../../Scripts/scriptMob.js" type="text/javascript"> 

to check whether browser actually reads the js file correctly.

检查浏览器是否真正正确读取了 js 文件。

the error message was saying: [SqlException]: Error converting data type nvarchar to int.

错误消息说:[SqlException]:将数据类型 nvarchar 转换为 int 时出错。

回答by TechTalk on WRLR 98.3FM

Had a similar problem, shoving my head against a wall trying to figure it out. Turned out the js file had a syntax issue buried in it, something stupid that didn't show in my editor ... in this case it was a hanging 'var ' with nothing after it. As soon as I deleted that, everything was copacetic.

有一个类似的问题,我把头靠在墙上试图弄清楚。结果发现 js 文件有一个语法问题,一些愚蠢的东西没有显示在我的编辑器中……在这种情况下,它是一个挂起的“var”,后面什么都没有。一旦我删除了它,一切都变得扑朔迷离。

So my recommendation if you run into this is what I did - remove all functions and add them back one by one until you find one that kills it.

因此,如果您遇到这种情况,我的建议就是我所做的 - 删除所有功能并将它们一个一个地添加回来,直到找到一个可以杀死它的功能。

回答by polearnik

Had a similar problem, cant load script /js/search_ads.js It was problem in my adblock extension After renaming to search.js problem was disappearing

有类似的问题,无法加载脚本 /js/search_ads.js 这是我的 adblock 扩展中的问题 重命名为 search.js 问题消失后

回答by Rocket Ronnie

charset isnt a valid attribute for the script tag, try removing it

字符集不是脚本标记的有效属性,请尝试将其删除

EDIT

编辑

Actually it is valid, try removing it anyway :)

实际上它是有效的,无论如何尝试删除它:)

回答by Hooray Im Helping

Make sure you've closed all your script tags above the one you're trying to load.

确保您已关闭要加载的脚本标签上方的所有脚本标签。