views:

176

answers:

3

Why are JS scripts usually place in the header of a document? Is it required by standards, or is it just a convention with no particular reason?

+3  A: 

It's just a convention. It's usually recommended to put scripts at the end of the body so the page can display before loading them, which is always a plus. Also, document.body can't be used until the document is loaded or if you put the script in the body.

Eli Grey
It's not just a convention, there's a difference.
Maiku Mori
+9  A: 

See http://developer.yahoo.com/performance/rules.html#js_bottom

Although past practice has often been to place them in the header for the sake of centralizing scripts and styles (and the like), it is advisable now to place the scripts at the bottom to improve loading speed of the rest of the page.

To quote:

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.

In some situations it's not easy to move scripts to the bottom. If, for example, the script uses document.write to insert part of the page's content, it can't be moved lower in the page. There might also be scoping issues. In many cases, there are ways to workaround these situations.

An alternative suggestion that often comes up is to use deferred scripts. The DEFER attribute indicates that the script does not contain document.write, and is a clue to browsers that they can continue rendering. Unfortunately, Firefox doesn't support the DEFER attribute. In Internet Explorer, the script may be deferred, but not as much as desired. If a script can be deferred, it can also be moved to the bottom of the page. That will make your web pages load faster.

Jonathan Fingland
+3  A: 

A <script src="url"></script> will block the downloading of other page components until the script has been fetched, compiled, and executed. It is better to call for the script as late as possible, so that the loading of images and other components will not be delayed.

It depends on what the script is doing. If your code is wrapped in onLoad event then it doesn't matter since it will return almost immediately and not block otherwise you should put it where it fits because the placement does matter.

As for putting it at the end, it does give a little extra time for user to start looking at the page. Just ask yourself a question - does my site work without javascript? If it doesn't, then in my opinion it doesn't mater where you put it since onLoad code will only be executed when the DOM has been fully loaded (that includes binary content like images). If you can use it without javascript then put it at the end so that images can load faster.

Also note that most JS libraries use special code which works around the onLoad problem and uses custom event for this which gets fired once DOM has loaded and doesn't wait for binary data.

Now that I wrote all that, I got a question of my own. Does using say jQuery's

$(document).ready(function () {});

and putting the script tag at the end of page is the same as using onLoad event and putting it at the start? It should be the same because browser would load all images before loading the script which is the last one in the list. If you know the answer leave a comment (I'm too lazy and it's too late to test it atm).

Maiku Mori
$(document).ready != window.onload, it uses the DOMready event which is fired when the page DOM has been built, onload is generally when the page has rendered (atleast in IE, FF etc. work differently). See jQuery docs on the ready event for more.
roryf
I know, read my full answer again. The thing is that in theory it should act the same as onLoad if it's used in a script file which is included at the end of page. Since bowser should load DOM and all images first before even requesting the script from web-server. And my question is if this assumption is actually correct.
Maiku Mori
Not quite: the browser should have _started_ loading all of the images in this case, but the DOMready event will fire immediately when the HTML has been fully parsed. The images will normally still be loading for a while. onLoad fires when everything has been fully loaded.
Teun D
Yeah, but the browser should query the script file only after he has done with most of the images because it's last in the queue, shouldn't it? If that's so then DOM has been build ages ago and browser already has most of the images at the time when the script is being parsed and executed.
Maiku Mori
I'll make a test soon.
Maiku Mori