LoadJS
LoadJS is a tiny async loader for modern browsers (738 bytes).
Introduction
LoadJS is a tiny async loading library for modern browsers (IE9+). It has a simple yet powerful dependency management system that lets you fetch JavaScript and CSS files in parallel and execute code after the dependencies have been met. The recommended way to use LoadJS is to include the minified source code of loadjs.js in your <html> (possibly in the <head> tag) and then use the loadjs
global to manage JavaScript dependencies after pageload.
LoadJS is based on the excellent $script library by Dustin Diaz. We kept the behavior of the library the same but we re-wrote the code from scratch to add support for success/error callbacks and to optimize the library for modern browsers. LoadJS is 738 bytes (minified + gzipped).
Here's an example of what you can do with LoadJS:
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar');
loadjs.ready('foobar', {
success: function() { },
error: function(depsNotFound) { }
});
The latest version of LoadJS can be found in the dist/
directory in this repository:
You can also use it as a CJS or AMD module:
$ npm install --save loadjs
var loadjs = require('loadjs');
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar');
loadjs.ready('foobar', {
success: function() { },
error: function(depsNotFound) {}
});
Browser Support
- IE9+ (
async: false
support only works in IE10+) - Opera 12+
- Safari 5+
- Chrome
- Firefox
- iOS 6+
- Android 4.4+
LoadJS also detects script load failures from AdBlock Plus and Ghostery in:
Note: LoadJS treats empty CSS files as load failures in IE (to get around lack of support for onerror events on <link>
tags)
Documentation
- Load a single file
loadjs('/path/to/foo.js', {
success: function() { }
});
- Fetch files in parallel and load them asynchronously
loadjs(['/path/to/foo.js', '/path/to/bar.js'], {
success: function() { }
});
- Fetch files in parallel and load them in series
loadjs(['/path/to/foo.js', '/path/to/bar.js'], {
success: function() { },
async: false
});
- Fetch JavaScript and CSS files
loadjs(['/path/to/foo.css', '/path/to/bar.js'], {
success: function() { }
});
- Force treating file as CSS stylesheet
loadjs(['css!/path/to/cssfile.custom'], {
success: function() { }
});
- Add a bundle id
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', {
success: function() { }
});
- Check if bundle has already been defined
if (!loadjs.isDefined('foobar')) {
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', {
success: function() { }
});
}
- Add an error callback
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', {
success: function() { },
error: function(pathsNotFound) { }
});
- Retry files before calling the error callback
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', {
success: function() { },
error: function(pathsNotFound) { },
numRetries: 3
});
- Execute a callback before script tags are embedded
loadjs(['/path/to/foo.js', '/path/to/bar.js'], {
success: function() {},
error: function(pathsNotFound) {},
before: function(path, scriptEl) {
if (path === '/path/to/foo.js') scriptEl.crossOrigin = true;
}
});
- Bypass LoadJS default DOM insertion mechanism (DOM
<head>
)
loadjs(['/path/to/foo.js'], {
success: function() {},
error: function(pathsNotFound) {},
before: function(path, scriptEl) {
document.body.appendChild(scriptEl);
return false;
}
});
- Execute a callback after bundle finishes loading
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar');
loadjs.ready('foobar', {
success: function() { }
});
- Chain .ready() together
loadjs('/path/to/foo.js', 'foo');
loadjs('/path/to/bar.js', 'bar');
loadjs
.ready('foo', {
success: function() { }
})
.ready('bar', {
success: function() { }
});
- Compose more complex dependency lists
loadjs('/path/to/foo.js', 'foo');
loadjs('/path/to/bar.js', 'bar');
loadjs(['/path/to/thunkor.js', '/path/to/thunky.js'], 'thunk');
loadjs.ready(['foo', 'bar', 'thunk'], {
success: function() {
},
error: function(depsNotFound) {
if (depsNotFound.indexOf('foo') > -1) {};
if (depsNotFound.indexOf('bar') > -1) {};
if (depsNotFound.indexOf('thunk') > -1) {};
}
});
- Use .done() for more control
loadjs.ready(['dependency1', 'dependency2'], {
success: function() {
}
});
function fn1() {
loadjs.done('dependency1');
}
function fn2() {
loadjs.done('dependency2');
}
- Reset dependency trackers
loadjs.reset();
Directory structure
loadjs/
├── dist
│ ├── loadjs.js
│ ├── loadjs.min.js
│ └── loadjs.umd.js
├── examples
├── gulpfile.js
├── LICENSE.txt
├── package.json
├── README.md
├── src
│ └── loadjs.js
├── test
└── umd-templates
Development Quickstart
- Install dependencies
- Clone repository
$ git clone git@github.com:muicss/loadjs.git
$ cd loadjs
- Install node dependencies using npm
$ npm install
- Build examples
$ npm run build-examples
To view the examples you can use any static file server. To use the nodejs
http-server module:
$ npm install http-server
$ npm run http-server -- -p 3000
Then visit http://localhost:3000/examples
- Build distribution files
$ npm run build-dist
The files will be located in the dist
directory.
-
Run tests
To run the browser tests first build the loadjs
library:
$ npm run build-tests
Then visit http://localhost:3000/test
-
Build all files
$ npm run build-all