在哪里放置 JavaScript 函数:<head>?<身体>?或者,在 </html> 之后?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9520215/
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
Where to place JavaScript functions: <head>? <body>? or, after </html>?
提问by Doug Null
I'm confused about where to place JavaScript functions:
我对放置 JavaScript 函数的位置感到困惑:
When should they be put within the head When inline within the body And, when after the closing html tag?
什么时候应该将它们放在 head 内 何时内联在 body 中以及何时在关闭 html 标记之后?
thanks
谢谢
回答by JKirchartz
The rules are fast and loose on this, There is no right or wrong way only better and less-better. (well after the </html>
iswrong)
规则在这方面既快又松,没有对错之分,只有更好和更少更好。(好后</html>
是错误的)
Generally speaking, javascript in the head
of the document might block rendering of the page until the file is loaded in some browsers *cough*IE*cough*. This is due to a limit of simultaneous connections. So some people put them beforethe closing html
tag. You can use a library to asynchronously load the javascript to avoid this blocking.
一般来说,head
文档中的 javascript可能会阻止页面的呈现,直到文件在某些浏览器中加载 *cough*IE*cough*。这是由于同时连接的限制。所以有些人把它们放在结束标签之前html
。您可以使用库异步加载 javascript 以避免这种阻塞。
If you're using a library, or checking for the DOM to be loaded before executing code there's really no issue of where it's placed. However if you're not doing that it's probably better to put it at the end.
如果您正在使用库,或者在执行代码之前检查要加载的 DOM,那么放置它的位置实际上没有问题。但是,如果您不这样做,最好将其放在最后。
回答by George Cummins
Javascript can always be safely placed in the head to make the functionality available to the entire page. Note that this can block loading of the rest of the document, so if you are loading very large or external Javascript, you may wish to load them inline near the end of the body.
Javascript 总是可以安全地放置在头部,以使整个页面都可以使用该功能。请注意,这可能会阻止加载文档的其余部分,因此如果您正在加载非常大的或外部 Javascript,您可能希望在正文末尾附近加载它们。
Javascript placed inline will become available when executed. This allows you to conditionally load JS when page elements are loaded.
内嵌的 Javascript 将在执行时可用。这允许您在加载页面元素时有条件地加载 JS。
Javascript should always be placed in the <head>
or <body>
, never after </html>
.
Javascript 应该始终放在<head>
or 中<body>
,永远不要放在之后</html>
。
回答by folktrash
I agree, never seen (nor could I recommend) after html. Also, as noted, blocking is the concern. What I often go with is one script reference in the head to yepnope(an async js loader and testing tid bit (now included with modernizr)), and a tiny block of inline js at the end of the body tag that loads a bootstrap js file.
我同意,在 html 之后从未见过(也不能推荐)。此外,如前所述,阻塞是一个问题。我经常去的是在头一个脚本参考yepnope(异步JS装载机和测试TID位(现在包括Modernizr的)),和内嵌的JS在body标签的最后一个微小块负荷自举JS文件。
In that file, I use another yepnope request to load other needed assets asynchronously, and kick off initialization methods.
在该文件中,我使用另一个 yepnope 请求异步加载其他需要的资产,并启动初始化方法。
I've also taken to putting my Google Analytics code in a final yepnope block, so that it's the last thing to load, even after the js app boots.
我还把我的 Google Analytics 代码放在最后的 yepnope 块中,这样即使在 js 应用程序启动之后,它也是最后加载的东西。