javascript 在 HTML 页面中放置内部 JS 和外部 JS 的位置

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

Where to place Internal JS and External JS in HTML page

javascriptexternalinternal

提问by IT_INFOhUb

I have used internal JS(on page) in my page which I write for my coding purpose. Along with it I use External JS too. I do use of External JS in my Internal JS(on page). It works fine when I place External JS below my Internal JS. But It does not work when I place External JS above internal JS. I ask this for my knowledge. I just want to know reason. I know that.. JS should place at end of body tag. But what about when I use Internal as well as External JS.

我在我的页面中使用了内部 JS(在页面上),这是我为编码目的而编写的。除了它,我也使用外部 JS。我在我的内部 JS 中使用了外部 JS(在页面上)。当我将外部 JS 放在内部 JS 下方时,它工作正常。但是当我将外部 JS 放在内部 JS 之上时它不起作用。我问这个是为了我的知识。我只是想知道原因。我知道.. JS 应该放在 body 标签的末尾。但是当我使用 Internal 和 External JS 时呢?

---> If I define external js bellow internal js. Works fine.

---> 如果我在内部 js 下定义外部 js。工作正常。

<script type="text/javascript">
    script code .. which uses external.js function. 
</script>
<script type="text/javascript" src="external.js"></script>

---> If I define external js above internal js. Does not work.

---> 如果我在内部 js 之上定义外部 js。不起作用。

<script type="text/javascript" src="external.js"></script>
<script type="text/javascript">
     script code .. which uses external.js function.
</script>

回答by Halcyon

Typically you will want to place scripts, internal or external, in the <head>.

通常,您希望将内部或外部脚本放在<head>.

Google Analytics likes to be at the bottom of the pagefor reasons that make sense if you're slapping it onto an existing product. If you're designing scripts for a website from the get-go you have a chance to do it right.

谷歌分析喜欢在页面底部,如果你把它贴在现有产品上是有意义的。如果您从一开始就为网站设计脚本,那么您就有机会把它做好。

Whether a script is internal or external is usually determined by whichever method makes more sense. If you have a script that is used on multiple pages you will likely put it in an external script so you can reuse it. Performance considerations are quite complex, it's certainly no trivial matter.

脚本是内部的还是外部的,通常取决于哪种方法更有意义。如果您有一个在多个页面上使用的脚本,您可能会将它放在一个外部脚本中,以便您可以重用它。性能考虑相当复杂,这当然不是小事。

I guess what you're running into is some dependency of the external script on some internal script.

我猜您遇到的是外部脚本对某些内部脚本的某种依赖性。

<script type="text/javascript">
    var my_var = "foo";
</script>
<script type="text/javascript" src="external.js"></script>

-- external.js
console.log(my_var);

If you change the order of the scripts in the above example it will print undefinedinstead of "foo". Make sure you define your scripts in an order that makes sense. Make sure your external scripts don't have strange or undocumented dependencies.

如果您在上面的示例中更改脚本的顺序,它将打印undefined而不是"foo". 确保以合理的顺序定义脚本。确保您的外部脚本没有奇怪或未记录的依赖项。