I've read that it is better to place your <script>
tags at the end of the document. The argument for doing this appears to be that the browser will stall rendering the page below a script tag until it has loaded and and executed the script. If your script tag is at the top of the page, rendering is stalled for a while, which is bad.
However, I am not sure if this is really true any more.
Looking around, I normally see the following locations...
In the <head>
of the page or Just inside the <body>
tag
Stackoverflow is an example of a site that puts the script tags in the head
, and since they are normally rather obsessed with performance, I am starting to wonder if position in the page is important at all.
Last thing in the body
element
The other common place to put javascript appears to be right at the very end of the <body>
element. I am assuming this means that the page can render while the javascript downloads and gets on with doing its thing.
But which is better?
Does anyone have any thoughts or advice on this? I am looking to try and get our pages to perform and appear to the user as quickly as possible.
Does it matter? What are the advantages of being at the top of the page? Bottom of the page?