JavaScript on the bottom of the page?

百般思念 提交于 2019-11-26 22:39:24
Undefined

It is very important for best practice reasons.

When you have scripts loading in your header, they stop other downloads from taking place! This includes your styling, and will also stop your images from downloading until the script has finished.

This is because JavaScript files load synchronously.

Also note that you will get a flash of unstyled content (FOUT) during loading if you do not move your JavaScript files to the bottom of your page. This is because your CSS will not download until the script has finished loading.


Here is an excerpt from Yahoo performance rule 6.

The second problem caused by scripts is blocking 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. (I've gotten Internet Explorer to download over 100 images in parallel.) While a script is downloading, however, the browser won’t start any other downloads, even on different hostnames.


References

http://developer.yahoo.com/performance/rules.html/

Especially note rule 6.

We recently had this debate at the office. I wrote a lengthy post where I lay down my opinion on the subject. The short answer is that it really depends on what you're making. For content oriented web pages, bottom placement seems to work best. However when one is creating web applications where functionality is the main priority, placement at the top has it's advantages.

JavaScripts load synchronously. Pair that with generally larger file sizes, and you have content that is being delayed in loading because of the synchronous JavaScript loading. If you put the JavaScripts at the bottom of the page, then everything else is loaded first and the JavaScript loading can't block anything.

Basically, when the browser hits a <script> tag, it stops loading the rest of the document until that <script> is loaded and executed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!