tags:

views:

445

answers:

3

Does anyone use deferred execution of JavaScript or is it something from the past? (I don't see it used on major websites). Also, what are the pros and cons of placing your javascript in the head tag vs placing it in the body?

Thanks

+1  A: 

The big con is that it's implemented incredibly differently between browsers. Basically, you really just can't use it as it seems to be intended.

+3  A: 

Placing JavaScript in the head tag can slow down the load time of the page, since the JS has to be parsed, processed and executed (and possibly downloaded, if they are external files) before the DOM is rendered. Even using "defer" doesn't really avoid this - some browsers ignore the directive and the ones which do pay attention don't fully implement it as you'd expect.

Placing JS at the foot of the page is a great way to get some quick wins with apparent page load time, and also encourages proper SoC and progressive enhancement by making it a little harder to code improperly.

Rex M
+2  A: 

http://www.websiteoptimization.com/speed/tweak/defer/

For all those who want to know more about deferred execution.

It seems that this is a feature of HTML 4, so not necessarily from the past, but with the onload event doing pretty much what the deferred option asks, I'm not sure there's a huge demand for it.

altCognito
Actually I visited the same link before I ask this question, but thanks anyway :)
Waleed Eissa