我在哪里放jQuery脚本标签? [英] Where do I put the jQuery script tag?

查看:94
本文介绍了我在哪里放jQuery脚本标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在所有文档中,我在头部看到< title> 下面的jQuery脚本标记,但是当我进入其他一些网站时( initializr 模板是我头顶的第一个模板,它们将它放入身体的底部(你知道,就在<$之前) c $ c>< / body> )。

In all the documentation I see the jQuery script tag beneath <title> in the head, but then when I go into some other sites (the initializr template is the first off the top of my head), they drop it into the bottom of the body (you know, right before </body>).

这两个中的哪一个是对的?

Which of these two is right?

推荐答案

引用YDN 加速的最佳做法您的网站


将脚本置于底部

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

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.

在某些情况下,将脚本移到底部并不容易。例如,如果脚本使用document.write插入页面内容的一部分,则无法在页面中向下移动。可能还存在范围问题。在许多情况下,有办法解决这些问题。

In some situations it's not easy to move scripts to the bottom. If, for example, the script uses document.write to insert part of the page's content, it can't be moved lower in the page. There might also be scoping issues. In many cases, there are ways to workaround these situations.

经常出现的另一个建议是使用延迟脚本。 DEFER属性指示脚本不包含document.write,并且是浏览器可以继续呈现的线索。不幸的是,Firefox不支持DEFER属性。在Internet Explorer中,脚本可能会延迟,但不是所需的。如果可以延迟脚本,也可以将其移动到页面底部。这将使您的网页加载速度更快。

An alternative suggestion that often comes up is to use deferred scripts. The DEFER attribute indicates that the script does not contain document.write, and is a clue to browsers that they can continue rendering. Unfortunately, Firefox doesn't support the DEFER attribute. In Internet Explorer, the script may be deferred, but not as much as desired. If a script can be deferred, it can also be moved to the bottom of the page. That will make your web pages load faster.

人们常说我在2009年说过现在它突然变成了最佳实践......一些进步。

It's funny that people used to say that I should be slapped upside the head for doing that when I said that in 2009 and now it's suddenly a best practice... Some progress.

这篇关于我在哪里放jQuery脚本标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆