Dustin Diaz's $script.js

About 1 min reading time

Follow link to dustindiaz.com →

The Javascript ninja Dustin Diaz of Twitter has created a very, very minimal solution for loading Javascript files asynchronously as well with dependencies.

$script('analytics.js');
$script('library.js', function() {
  // do stuff with library.
});

Not too hard to grasp. Import library.js , use it in the code. For the jQuery fans:

$script('jquery.js', 'jquery');

$script.ready('jquery', function() {
  // do stuff with jQuery
});

It seems you’re able to do some pretty neat stuff with it:

new school - loads as non-blocking, and ALL js files load asynchronously
$script('jquery.js', 'jquery');
$script('my-jquery-plugin.js', 'plugin');
$script('my-app-that-uses-plugin.js');

/*--- in my-jquery-plugin.js ---*/
$script.ready('jquery', function() {
  // plugin code...
});

/*--- in my-app-that-uses-plugin.js ---*/
$script.ready('plugin', function() {
  // use your plugin :)
});

It adds a little bit more control over the browser loading process, and as a bonus it doesn’t block the downloading of images and other documents. Check out the blog post or follow the project on Github.