PDA

View Full Version : Sharing an Optimization Tip



TheMichael
21 Jul 2010, 12:03 AM
The following code is placed before the closing body tag (and not in the head, as is traditionally done).



<script type="text/javascript">
var DocumentHead = document.getElementsByTagName('head')[0];

function jsOptimizedLoader(src) {
var Script = document.createElement('script');
Script.src = src;
Script.type = 'text/javascript';
DocumentHead.appendChild(Script);
}

jsOptimizedLoader('js/foo.js');
jsOptimizedLoader('js/bar.js');
jsOptimizedLoader('js/etc.js');
</script>


Once your DOM has finished loading, all the scripts tags are added to the head via javascript. Allowing your page to display PRIOR to spending the time loading all of those scripts will win you usability points and ease of access points with your audience.

(You can find the more complete article in my blog at http://www.michaelhartmayer.com/javascript/loading-javascript-better/ if interested.)

Hope this helps someone else as much as it helped me.

Cheers

michaelangrave
21 Jul 2010, 07:02 AM
Nice advice,

I've not yet had the chance to test this, but am certainly keen to. Google is really focusing on page load time and quality, and this will continue to develop in importance.

I imagine this stems from, in my opinion, the under performance of internet from mobile devices, well certainly in the UK. Over 3G, my iPhone still seems to take an age to load heavy pages.

Michael.