DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat

Head.js is Awesome!

John Whish user avatar by
John Whish
·
Sep. 21, 12 · Interview
Like (0)
Save
Tweet
Share
9.41K Views

Join the DZone community and get the full member experience.

Join For Free

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.

 

 

 

 

Awesome (window manager)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Future of Cloud Engineering Evolves
  • The Quest for REST
  • Java Development Trends 2023
  • A Complete Guide to AngularJS Testing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: