LoadJS is a tiny async loader for modern browsers (884 bytes).
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, CSS and image 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.
-
Load a single file
loadjs('/path/to/foo.js', function() {
});
-
Fetch files in parallel and load them asynchronously
loadjs(['/path/to/foo.js', '/path/to/bar.js'], function() {
});
-
Fetch JavaScript, CSS and image files
loadjs(['/path/to/foo.css', '/path/to/bar.png', 'path/to/thunk.js'], function() {
});
-
Force treat file as CSS stylesheet
loadjs(['css!/path/to/cssfile.custom'], function() {
});
-
Force treat file as image
loadjs(['img!/path/to/image.custom'], function() {
});
-
Add a bundle id
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', function() {
});
-
Use .ready() to define bundles and callbacks separately
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar');
loadjs.ready('foobar', function() {
});
-
Use multiple bundles in .ready() dependency lists
loadjs('/path/to/foo.js', 'foo');
loadjs(['/path/to/bar1.js', '/path/to/bar2.js'], 'bar');
loadjs.ready(['foo', 'bar'], function() {
});
-
Chain .ready() together
loadjs('/path/to/foo.js', 'foo');
loadjs('/path/to/bar.js', 'bar');
loadjs
.ready('foo', function() {
})
.ready('bar', function() {
});
-
Use Promises to register callbacks
loadjs(['/path/to/foo.js', '/path/to/bar.js'], {returnPromise: true})
.then(function() { })
.catch(function(pathsNotFound) { });
-
Check if bundle has already been defined
if (!loadjs.isDefined('foobar')) {
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', function() {
});
}
-
Fetch files in parallel and load them in series
loadjs(['/path/to/foo.js', '/path/to/bar.js'], {
success: function() { },
async: false
});
-
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;
}
});
-
Use bundle ids in error callback
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'], function() {
});
function fn1() {
loadjs.done('dependency1');
}
function fn2() {
loadjs.done('dependency2');
}
-
Reset dependency trackers
loadjs.reset();
-
Implement a require-like dependency manager
var bundles = {
'bundleA': ['/file1.js', '/file2.js'],
'bundleB': ['/file3.js', '/file4.js']
};
function require(bundleIds, callbackFn) {
bundleIds.forEach(function(bundleId) {
if (!loadjs.isDefined(bundleId)) loadjs(bundles[bundleId], bundleId);
});
loadjs.ready(bundleIds, callbackFn);
}
require(['bundleA'], function() { });
require(['bundleB'], function() { });
require(['bundleA', 'bundleB'], function() { });