views:

87

answers:

3

Edit: While this question has been asked and answered before (1), (2), (3), the answers have not mentioned the possibility of using asynchronous and /or lazy loading when including files in the <head>. I was prompted to ask the question due to Google analytics new code which uses both of these methods.


I recently noticed that Google analytics is now suggesting including its Javascript snippet right before the </head> tag. They used to suggest including the snippet right before the </body> tag.

The YUI Best Practices for Speeding Up Your Web Site suggests putting scripts as far down the page as possible, since scripts can block parallel downloads:

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.

Google says:

One of the main advantages of the asynchronous snippet is that you can position it at the top of the HTML document. This increases the likelihood that the tracking beacon will be sent before the user leaves the page. It is customary to place JavaScript code in the <head> section, and we recommend placing the snippet at the bottom of the <head> section for best performance.

I'm generally more concerned with user experience and page load speed than making sure each and every tracking beacon is sent, so this would push me toward including the Google analytics script toward the bottom of the page, instead of in the <head>, right?

I'm sure there's more things to consider than these two points of view. What are the things influencing you? What are the things to consider?

So, what are the pros and cons of having your scripts right berfore </head> versus right before </body>?

+4  A: 

The advice about <head> is to not LINK TO EXTERNAL scripts that need to be downloaded. This blocks parallel downloads. Google's newest tracking code uses lazy loading, and doesn't block parallel downloads.

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
Mike Sherov
+1  A: 

Putting scripts in the <head> will force the browser to download files before it renders the page and blocks parallel downloads. If you place your scripts right before the end </body> tag, the browser will parse those after your content, which should result in a faster page load.

By using the self executing anonymous function with async=true, you don't block the parallel downloading.

For really complex applications with modals, a con of putting a script right before the </body> would be if you needed to hide a modal if JS is enabled.

<head>
<script>document.documentElement.className+='js';</script>
<style>html.js #modal { display:none; }</style>
</head>

If the above snippet was placed before </body> it won't be parsed as soon as if it was in the head. There would be certain cases and inconsistencies in terms of dom ready timing that you might notice as well.

Related question: http://stackoverflow.com/questions/1013112/where-should-i-declare-javascript-files-used-in-my-page-in-head-head-or-near

meder
+2  A: 

My guess, in addition to what the other people have said so far, is so that the analytics can more accurately track visits. Sometimes someone will go to a site and leave before the whole page has downloaded. If they do this, there's a greater chance that they've downloaded the tracking code if it's nearer the top of the page. This should help those analyzing the statistics to see bounce rates. If you notice your bounce rate is high (and time on page is low), it could be an indicator that your page is taking too long for most audiences and should alert you to do something to speed up your page load.

Jason