
From the dawn of web development we have been told javascript calls to external files (and page level too) should be placed in the head of your
document along with CSS, title, meta etc. Although this is not wrong a perceived page load to the user may seem longer with js at the top, let me explain:
First off some facts:
- XHTML strict doctypes (and others) allows for the <script /> tag anywhere in the DOM
- Web browsers stop page rendering operations to process javascript
- If your javascript is written properly JavaScript functionality won’t start it’s interactions until after page load
Web browsers download assets simultaniously until javascript is encountered, it then stops everything, gets the javascript, processes the script contents. You can probably see where this is going, if the svrips are in the <head> of the document all your javascript has to process before a single image, form element, <p> has rendered. The user waits, it processes, and once the js is processed we see the page come in.
Why is this a problem you ask when on a high speed connection you can barely notice?
Two reasons:
1. not everyone has a high speed connection. Through Critical Mass I have done work for some very large companies that have large user bases outside North America where often their “high speed” isn’t so speedy
2. Web 2.0 ( I hate that term, but I will use it ) is very javascript heavy, it seems that every project there is more and more interactions that are somehow touched by javascript. Keep the experience as snappy for the user as possible regardless of what we perceive their actual download experience to be.
Best practice would dictate that regardless of the size of the javascript download, having the page render so the user can see something happening then add your javascript events on page load at the bottom of the page.
Do your users a favour, don’t make them wait for javascript, let them see all your content and pretty pictures sooner! They will thank you*.
*Users may not actually thank you, but you can feel happy that you made your site better for them
If you are interested in this and more techniques, I didn’t think this up and you can see a list of them at: Yahoo! Exceptioal Web Site Performance Rules
Also you can check the efficiency of yours (and others) pages using: YSlow for Firebug
Disclaimer: this WordPress theme is not something I have built and I have not built in these techniques for froosh.ca, my personal site is always the last thing that gets attention.
Comment