外部 JavaScript - 身体还是头部?

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

External JavaScript - body or head?

javascripthtml

提问by Jason Stackhouse

Possible Duplicate:
Where is the best place to put <script> tags in HTML markup?

可能的重复:
在 HTML 标记中放置 <script> 标签的最佳位置在哪里?

Where should I put my external JavaScript file? I know that people put it at the end of the body tag to make the web page look like it loads faster. But is there any cons about putting it at the end?

我应该把我的外部 JavaScript 文件放在哪里?我知道人们把它放在 body 标签的末尾是为了让网页看起来加载速度更快。但是把它放在最后有什么坏处吗?

And would this be a good practice to put the JavaScript with the Google Analytics code?

将 JavaScript 与 Google Analytics 代码一起使用是否是一个好习惯?

<body>
// Everything else over here ... conent etc..
     <script src="myjavascript.js" type="text/javascript"></script>
        <script type="text/javascript">
        // google analytics code
        </script>
</body>

采纳答案by rgvcorley

Yes people usually put it at the end for faster page loads. What you have there with the google analytics script is common practice.

是的,人们通常将它放在最后以加快页面加载速度。你在谷歌分析脚本中有什么是常见的做法。

You might also want to check out head.js- this has been shownto be even faster than a single script put at the end of the body

您可能还想查看head.js- 这已被证明比放在正文末尾的单个脚本还要快

回答by Oded

Current recommendations are to place the javascript at the bottom not because it "looks like it loads faster", but because by placing them there, javascript parsing and execution doesn't stop the browser from doing other things (like loading the rest of the page).

当前的建议是将 javascript 放在底部不是因为它“看起来加载速度更快”,而是因为将它们放在那里,javascript 解析和执行不会阻止浏览器做其他事情(比如加载页面的其余部分) )。

The one con I can think of is that if you define any objects and functions in external JS and want to use them in the page, you must wait for page load/ready.

我能想到的一个缺点是,如果你在外部 JS 中定义了任何对象和函数,并想在页面中使用它们,你必须等待页面加载/就绪。

As for the Google analytics code - it is good practice to place it at the bottom, as in your example.

至于谷歌分析代码 - 将它放在底部是一种很好的做法,就像你的例子一样。

回答by RobB

Placing your scripts at the end of your page will help improve the performance.

将脚本放在页面的末尾将有助于提高性能

The problem caused by scripts is that they block parallel downloads. The HTTP/1.1 specification suggests that browsers download no more than two components in parallel per hostname. If you serve your images from multiple hostnames, you can get more than two downloads to occur in parallel. While a script is downloading, however, the browser won't start any other downloads, even on different hostnames.

脚本引起的问题是它们会阻止并行下载。HTTP/1.1 规范建议浏览器每个主机名并行下载的组件不超过两个。如果您从多个主机名提供图像,则可以同时进行两次以上的下载。然而,在下载脚本时,浏览器不会启动任何其他下载,即使在不同的主机名上也是如此。

Its important to note that before you can access any objects referenced in the external JS they must be fully loaded first.

需要注意的是,在您访问外部 JS 中引用的任何对象之前,它们必须首先完全加载。