Possible Duplicate:
Is it practically good to put JS files at the bottom of webpage?
I’ve seen HTML documents where the developers place the <script src="xxx"> tags in the bottom of the document and is wondering why.
I’ve just read HTML4 specification regarding the script tag. It says nothing about when and how the script should be loaded. Hence it’s up to the web browser to handle that as it sees fit.
Isn’t it reasonable to think that the browser authors should be aware of that loading scripts synchronously or in any other way hinder the document rendering would affect the browsing experience?
i.e. aren’t we as web developers better off to put the scripts in the <head> tag?
Scripts require interpretation (which can be comparatively expensive) and may optionally require retrieval via another HTTP request. Placing them at the end of the document allows the rest of the page to load first.
https://developer.mozilla.org/en-US/docs/HTML/Element/script
See also http://dev.w3.org/html5/spec/the-script-element.html, specifically the areas around preparation/blocking which are covered in detail. Note that this is an editor’s draft in flux; some of these rules may not be in the final specification or be enforced by all user agents.
As a side note, this blocking behavior is not bad/wrong. Think of a library like Modernizr, which does belong in the
head. It alters the DOM in a way that allows CSS to be correctly applied; were it executed in parallel, the results would be incorrect.