Where to place JavaScript in an HTML file?

前端 未结 12 2214
萌比男神i
萌比男神i 2020-11-22 04:30

Say I have a fairly hefty JavaScript file, packed down to roughly 100kb or so. By file I mean it’s an external file that would be linked in via

12条回答
  •  粉色の甜心
    2020-11-22 04:59

    As a rule of thumb, the best place to put

    Why?

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

    CSS

    A little bit off-topic, but... Put stylesheets at the top.

    While researching performance at Yahoo!, we discovered that moving stylesheets to the document HEAD makes pages appear to be loading faster. This is because putting stylesheets in the HEAD allows the page to render progressively. More...

    Further reading

    Yahoo have released a really cool guide that lists best practices to speed up a website. Definitely worth reading: https://developer.yahoo.com/performance/rules.html

提交回复
热议问题