Aliaspooryorik
ColdFusion ORM Book

Head.js is awesome!

I've tweeted about head.js a few times and how much I like it, but I thought I should blog it as well to spread the word a bit.

Head.js does lots of clever things, but the part I'm particualry interested in is script loading. If you visit the head.js site there is a good example using the apple.com website to show how the scripts loading block subsequent requests. Because of this it's become considered best practice to place <script src="xyz"></script> tags at the bottom of the page, so that other assets are loaded in before the blocking javascript requests begin. Another popular technique is to combine multiple Javascript files into one file to improve performance. According to the head.js website, this is a misconception. Here's a quote:

There is a common misbelief that a single combined script performs best. Wrong:

  • latest browsers and Head JS can load scripts in parallel. loading 3 parts in parallel instead of as a single chunk is usually faster.
  • if an individual file is changed the whole combination changes and you lose the benefits of caching. It’s better to combine only the stable files that doesn’t change often.
  • many popular libraries are hosted on CDN. you should take the advantage of it instead of hosting yourself.
  • iPhone 3.x cannot cache files larger than 15kb and in iPhone 4 the limit is 25kb. And this is the size before gzipping. if you care about iPhones you should respect these limits.

With Head JS the file amount is not as critical as it is with SCRIPT SRC because page rendering is not blocked. Combining scripts is an important optimization method but you can go too far with it.

http://headjs.com/#theory

These are all great points!

So, how do you use it?

Consider this code:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Prototype</title>
<link rel="stylesheet" href="assets/css/core.css">
<!--[if lt IE 9]>
<script type="text/javascript" src="js/html5shiv.js"></script>
<![endif]-->

</head>
<body>

<header class="row">
An Awesome Website
</header>

<section id="content">
<article>#my.view.shows.here#</article>
</section>

<!-- load libraries -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
<!-- load resources -->
<script src="js/global.js"></script>
<!--- do funky stuff - must go after jQuery library loads --->
<script>
jQuery(function($){
// make header pulsate 'coz we kool dawg
$('header').effect("pulsate", { times:3 }, 2000);
});
</script>

</body>
</html>

OK, so this is pretty standard stuff that you'd have in a common layout file (you do use an MVC framework don't you?). One thing I will point out is that I'm not pulling html5shiv from http://html5shiv.googlecode.com/svn/trunk/html5.js. Why? well, there's a great article entitled 'html5shiv and Serving Content From Code Repositories' about why it's bad, but in short:

  • It’s Not Served Using HTTP Compression
  • It’s Not Cached
  • It’s Not the Most Recent Version
  • You’re linking to a source code repository

Anyway that was an aside (Ha - what a witty pun!). Getting back to the purpose of this post…

One problem I have with putting all my JavaScript includes at the bottom of the page is that I can't have ad-hoc JavaScript in my views that requires one of those libraries as that library isn't loaded in the document sent to the browser (view + layout combined). For example if my view contains something like:


<script>
jQuery(function($){
$('#someelement').text('jQuery loaded and ready to rock!');
});
</script>

I'd get a nice JavaScript error telling me that jQuery is undefined. Because of this, I often end up loading jQuery etc in the head anyway.

If I rewrite the above HTML page using head.js I get this:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Prototype</title>
<link rel="stylesheet" href="assets/css/core.css">
<script type="text/javascript" src="js/head.js"></script>
</head>
<body>

<header class="row">
An Awesome Website
</header>

<section id="content">
<article>Really interesting stuff</article>
</section>

<!--- do something --->
<script>
head(function(){
// make header pulsate 'coz we kool dawg
$('header').effect("pulsate", { times:3 }, 2000);
});
</script>

<!-- load libraries -->
<script>
head.js(
"//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js",
"//ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js",
"js/global.js"
);
</script>

</body>
</html>

Note that the html5shiv has gone - Head.js does that for me. I'm also now getting head.js to load the files in parallel, but, execute them in sequence using the head.js() method. Note that I said in parallel - they are now no longer blocking.

The other crazy cool thing is that I can have JavaScript code that has a dependancy on a library in the page before I load the library in. This is achieved using the head(function(){}) syntax in place of the jQuery(function(){}) syntax. What head(function(){}) does is ensure that the code inside the block isn't executed until all the libraries have loaded. In fact you can even get head.js to execute different code blocks depending on which library has loaded (see the section 'Labeling scripts' in the docs).

Anyway, I hope this has been of interest. Head.js is now an essential part of every project I work on.


6 comments

  1. Hey John, caught your tweet yesterday and went to check it out. Thanks for the heads up (pun intended). Going to use this in my next project.

    Comment by Jeff Fleitz – September 13, 2012
  2. head.js used to be my script loader of choice as well, however, I ran into a very serious bug where head.ready() doesn't always fire on IE9. Looking at the github repo and the issues list it seems like the project has been abandoned as well.

    I replaced head.js with lab.js (https://github.com/getify/LABjs) in all my projects and have had a lot of success with it. Its very similar to head.js, but without the IE issues. However, it's important to note that the creator of LAB.js has deemed it stable and has decided to stop development in favor of AMD.

    Comment by Ryan Anklam – September 13, 2012
  3. So from that is your shiv now in head.js and if so how do you handle the "if lt IE 9"

    Comment by Andy J – September 13, 2012
  4. Nice one, thanks for the article.

    You do use a client side MVC framework don't you? lol... like Sencha or alike MVC client frameworks...

    if you used ExtJS in your front end you could load dependant views on demand out of the box - ExtJS does the ordering for you... just like xero.com does.

    Comment by Dawesi – September 14, 2012
  5. @Jeff - have a look at the comment Ryan Anklam has made. I've not seen the issue with IE myself, but Ryan knows much more about JavaScript than I do.

    @Ryan - thanks for the heads up. I've experimented with quite a few frameworks today to see which I prefer but for a simple drop for in for head.js, LAB.js does seem to be a good fit.

    @Andy - head.js detects the browser vendor and version and creates a shiv if required.

    @Dawesi - I've been looking into MVC client side more and more, currently I'm using AngularJS which I really like, but does have limited documentation.

    Comment by John Whish – September 14, 2012
  6. Hi,

    Actually head.js ist working fine, except in IE. Here it throws me an error >
    "head is undefinded".

    Im loading version 0.99 with
    ...
    head.js("script1.js","script2.js","script3.js");

    Any idea what could cause this?


    thx
    Florian

    Comment by Florian – January 10, 2013

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.

Please subscribe me to any further comments
 

Search

Wish List

Found something helpful & want to say ’thanks‘? Then visit my Amazon Wish List :)

Categories

Recent Posts