HTML脚本标签的位置? [英] HTML script tag placement?
问题描述
每隔一段时间,我就会听到有关稍后将HTML < script>
标记放置在HTML文档中而不是在< head>
元素中的消息.
Every once in a while I hear about placing HTML <script>
tags later in the HTML document rather than in the <head>
element.
大约有人听说过,甚至提倡将自己的脚本放在</body>
之前的最后几个标签中.
Some people, or so I've heard, even advocate placing one's scripts as the last few tags before </body>
.
这是由于性能问题引起的吗?考虑到脚本相关脚本被放置在其他脚本之后,也许加载脚本是一项阻塞的IO操作:
Is this due to a performance issue? Perhaps loading up a script is a blocking IO operation, considering that script-dependent scripts are placed after other scripts like so:
<script src="jQuery.js"></script>
<script src="myScriptThatUsesjQuery.js"></script>
即使是这样,为什么将自己的脚本放在HTML文档的末尾也有帮助呢?
Even if that's the case, why would placing one's scripts near the end of the HTML document help?
谢谢!
推荐答案
当< script>
标签出现在< body>
中时,它会暂停解析文档,直到它被加载(如果适用)并执行为止.这样一来,使用 document.write
的旧脚本就可以发挥作用.
When a <script>
tags appears within <body>
, it pauses parsing of the document until it's been loaded (if applicable) and executed. This is so that old scripts which use document.write
can do their thing.
将< script>
标签放置在主体的最后,以防止它们支撑住东西.
Placing <script>
tags last in the body keeps them from holding things up.
这篇关于HTML脚本标签的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!