What is the difference between including Javascript in the <head> as opposed to immediately after the OPENING <body> tag?
Facebook Like widget is one example where they suggest placing the code immediately after the opening <body> tag. SiteCatalyst also suggests this for their analytics code.
While there is an obvious difference between including the code in the <head> element as opposed to right before the closing </body> tag, I’m not sure what the difference is between the <head> and immediately after the opening <body> tag.
If the JavaScript is outputting something into the DOM without a helper function (directly writing HTML), it would be important to have that JavaScript inside the
<body>, however there should be no difference in this case wither it is right after<body>or right before</body>.Typically, anything that is dependent on the DOM being loaded goes right before
</body>because until the page renders to that point, it would be impossible for the full DOM to be loaded. My guess would be that these scripts don’t depend on the DOM being loaded, but they do output HTML directly to the DOM (like a hidden<input>tag or something).