JavaScript on the bottom of the page?

后端 未结 4 453
野性不改
野性不改 2020-11-28 12:50

I\'ve read that it is better to keep all of your JavaScript files on the bottom of the webpage. The HTML5 Boilerplate template seems to agree: http://html5boilerplate.com/

4条回答
  •  無奈伤痛
    2020-11-28 13:12

    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.

提交回复
热议问题