Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Head.js is Awesome!

DZone's Guide to

Head.js is Awesome!

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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.

 

 

 

 

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:

Published at DZone with permission of John Whish, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}