I prefer to put links to external scripts in my head and scripts that launch things and initialize widgets and something else in the body.
A problem that is very easy to work with is that the script element in the body cannot access the elements that appear after it. In addition, the unpleasant browser compatibility issue associated with this is that IE does not allow script elements to modify the element in which they reside. Therefore, if you have this:
<div id="foo"> <script type="text/javascript"> document.getElementById("foo")... </script> </div>
IE will not like your page. Older versions of IE used very cryptic error messages for this or even an empty whole page, but IE8 seems to give a descriptive error message.
As long as you make sure that your scripts have access only to the DOM, which is safe to access, I do not think that it is evil to place script elements in the body. In fact, IMHO, placing scripts that initialize widgets after related items can be more readable than putting them in just one place (and I believe that it can also make them work earlier, which makes things roll less as the page loads).
Matti Virkkunen May 14, '10 at 22:14 2010-05-14 22:14
source share