Using Head.js to Load All Your JavaScript

In the past few years, client side frameworks/libraries have become very powerful and offers great features to implement complex functionalities on the client side, instead of relying on server side. This is really great for developers, but it has also made life difficult when it comes to managing multiple resources. One disadvantage is that loading multiple JavaScript file makes the site slower to load. This is because scripts block the rendering of the page until they are downloaded, parsed and executed. This can be fixed by using a Script loader library, and of these Head.js is the most popular. This post shows how to use Head.js to load all your JavaScript, including third party libraries like jQuery.

First of all, let’s take a look at what Script loader is and how it works…

What is Script Loader?

There is no concrete definition for Script loader, but one can think of Script loader as a library that helps in loading multiple JavaScript files without blocking the page rendering. Script loader will load the script files asynchronously, which results in non-blocking of page rendering. For the sake of argument, combining all your script files, then minifying them in a single file will definitely provide better performance, compare to loading multiple files. But that will also result in blocking of the page.

While using Script Loader, one thing to note is that your JavaScript code should only run when all your script files are loaded. There is no guarantee when a particular file will be loaded, so it is best to ensure that they are all loaded before running.

Now, let’s take a look at head.js


Head.js is a tiny script to simplify, speed up, and modernize your site. It loads JS & CSS asynchronously and in parallel, but executes them in order. It is cross-browser compatible, makes use of media queries to support responsive design and also supports feature detection like modernizr. To use it, download the library and include its reference in thesection. Finally, load all your scripts using the load method which accepts a list of script files and provides a callback function to indicate loading is completed. Like this:

head.load(‘file1.js’,’file2.js’, function(){ console.log(‘loading completed’); });

It also provides event listeners like jQuery’s document.ready(), called head.ready(). If you don’t specify the callback function in load method, then use head.ready() which indicates that loading of scripts is finished. Like this:

head.ready(function () {
      // loading finished.

There is no difference between using a callback with load method or using it separately via the head.ready(). It’s purely developer preference.

Head.js offers many useful features and one of the best features is the ability to label the scripts and execute code once the label is loaded, irrespective of other file’s loading status. The following code will create the label:

head.load({ jQuery: "scripts/jquery.js" },
{ tweet: "scripts/twitter.js" });

In the above code, there are 2 labels. One named “jQuery” for jQuery library and another called “tweet” for a twitter library. You can name it anything. Now, let’s say you want to execute the piece of code (completely dependent on jQuery) as soon as jQuery is loaded (irrespective of twitter loading status), you can check for the label in the head.ready() listener. Like this:

 head.ready("jQuery", function() {
 //code that dependent on jQuery

You can also pass a file name, instead of label in head.ready() and achieve the same thing.

 head.ready("jquery.js", function() {});


Script loader can help improve the loading process for any web page as scripts are loaded in a non-blocking way and asynchronously. Head.js is the tiniest library available today, and even though it has not been updated in the last 3 years, it still works well in all modern browsers. This post provides an overview about using Head.js and then labelling your script for executing the label dependent piece of code.

Recently Published

article image

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of checkbox input fields ...

article image

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...

article image

Add Keyboard Shortcut to Anchor Tag Using jQuery

Making a user friendly website can be quite a challenging task. There ...

Screen Shot 2017-04-23 at 10.15.41 AM

How to Use jQuery to Create a Sticky Navigation on Scroll

Creating “sticky” elements is something that can often be ...

article image

How to Use jQuery’s Keystroke Methods

jQuery has a few built-in keystroke methods that are all triggered by ...

article image

How to Change iFrame Styling Using jQuery

If you’ve ever had the displeasure of working with iFrames, ...