jquery - Jquery 代码应该放在页眉还是页脚中?

标签 jquery header footer

放置 Jquery 代码(或单独的 Jquery 文件)的最佳位置在哪里?如果我将其放在页脚中,页面加载速度会更快吗?

最佳答案

<强> Put Scripts at the Bottom

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. 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.

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.

编辑: Firefox 从 3.6 版开始就支持 DEFER 属性。

来源:

关于jquery - Jquery 代码应该放在页眉还是页脚中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28252104/

相关文章:

javascript - React H1 绘制空白

html - 100% 高度的 float 页脚?

PowerShell:CLI 中的页眉、页脚和/或框架

html - css页脚的问题

javascript - jQuery/Ajax - 成功消息后重定向到另一个页面

javascript - 在 jQuery 追加方法中更改文本颜色?

javascript - 无法检索通过 js 发送的数据

iOS UITableView : Empty header and footer height?

JavaScript JSON 数据问题 - 警报未在每个循环中显示

html - Bootstrap 3 两个带有大 Logo 的菜单标题