Manage JavaScript Dependencies
Posted on April 15th, 2008, by Cristian in Development, JavascriptJon Davis created Using.js, a simple library to manage dependencies with the goals of:
- Separate script dependencies from HTML markup (let the script framework figure out the dependencies it needs, not the designer)
- Make script referencing as simple and easy as possible (no need to manage the HTML files)
- Lazy load the scripts and not load them until and unless they are actually needed at runtime.
To use the script you simply:
// potential scripts are pre-registered first
using.register("jquery", "/scripts/jquery-1.2.3.js");
// later, when actually needed
using("jquery"); // loads jQuery and de-registers jQuery from using
$("a").css("text-decoration", "none");
// or asynchronously
using("jquery", function() {
$("a").css("text-decoration", "none");
});
As we see more and more tactics for getting performance by doing tricks with when scripts are loaded, I expect to see more of libraries like this. The key is working out exactly what script needs to be loaded right away, after the DOM is around, and what can wait for later. How do you want to load the script? Dynamic script element? Via iframe? XHR + eval? They all have pros and cons.
