What is loadjs?
loadjs is a JavaScript library for dynamically loading JavaScript and CSS files. It provides a simple API for loading scripts and stylesheets asynchronously, with support for callbacks and error handling.
What are loadjs's main functionalities?
Loading a single JavaScript file
This feature allows you to load a single JavaScript file asynchronously. The callback function is executed once the script is successfully loaded.
loadjs('path/to/script.js', function() { console.log('Script loaded!'); });
Loading multiple JavaScript files
This feature allows you to load multiple JavaScript files asynchronously. The callback function is executed once all the scripts are successfully loaded.
loadjs(['path/to/script1.js', 'path/to/script2.js'], function() { console.log('Scripts loaded!'); });
Loading CSS files
This feature allows you to load a CSS file asynchronously. You can provide success and error callbacks to handle the loading status.
loadjs('path/to/styles.css', { success: function() { console.log('CSS loaded!'); }, error: function(pathsNotFound) { console.log('CSS failed to load: ' + pathsNotFound); } });
Named bundles
This feature allows you to group multiple files into a named bundle. You can then use the `loadjs.ready` method to execute a callback once the entire bundle is loaded.
loadjs(['path/to/script1.js', 'path/to/script2.js'], 'bundleName'); loadjs.ready('bundleName', function() { console.log('Bundle loaded!'); });
Error handling
This feature provides error handling for loading scripts. You can specify a callback function to handle cases where one or more scripts fail to load.
loadjs(['path/to/script1.js', 'path/to/nonexistent.js'], { success: function() { console.log('Scripts loaded!'); }, error: function(pathsNotFound) { console.log('Scripts failed to load: ' + pathsNotFound); } });
Other packages similar to loadjs
requirejs
RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in other JavaScript environments, such as Rhino and Node. Compared to loadjs, RequireJS offers more advanced module management and dependency resolution.
headjs
HeadJS is a JavaScript library that allows you to load scripts and stylesheets asynchronously. It provides a simple API for managing dependencies and executing callbacks. Compared to loadjs, HeadJS offers similar functionality but with a different API design.
scriptjs
ScriptJS is a JavaScript loader that allows you to load scripts asynchronously. It provides a simple API for loading single or multiple scripts and handling callbacks. Compared to loadjs, ScriptJS focuses solely on script loading without support for CSS files.
LoadJS
LoadJS is a tiny async loader for modern browsers (534 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 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 in your <html> 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/failure callbacks and to optimize the library for modern browsers. LoadJS is 534 bytes (minified + gzipped).
Here's an example of what you can do with LoadJS:
loadjs(['foo.js', 'bar.js'], 'foobar');
loadjs.ready('foobar', function() {
});
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.
Browser Support
- IE9+
- Opera 12+
- Safari 5+
- Chrome
- Firefox
- iOS 6+
- Android 4.4+
Documentation
loadjs('foo.js', function() {
});
loadjs(['foo.js', 'bar.js'], function() {
});
loadjs('foo.js', function() {
loadjs('bar.js', function() {
});
});
loadjs(['foo.js', 'bar.js'], 'foobar', function() {
});
loadjs(['foo.js', 'bar.js'],
'foobar',
function() { },
function(pathsNotFound) { });
loadjs(['foo.js', 'bar.js'], 'foobar');
loadjs.ready('foobar', function() {
});
loadjs('foo.js', 'foo');
loadjs('bar.js', 'bar');
loadjs
.ready('foo', function() {
})
.ready('bar', function() {
});
loadjs('foo.js', 'foo');
loadjs('bar.js', 'bar');
loadjs(['thunkor.js', 'thunky.js'], 'thunk');
loadjs.ready(['foo', 'bar', 'thunk'],
function() {
},
function(depsNotFound) {
if (depsNotFound.indexOf('foo') > -1) {};
if (depsNotFound.indexOf('bar') > -1) {};
if (depsNotFound.indexOf('thunk') > -1) {};
});
loadjs.ready('my-awesome-plugin', function() {
myAwesomePlugin();
});
loadjs.ready('jquery', function() {
window.myAwesomePlugin = function() {
if (!window.jQuery) throw "jQuery is missing!";
};
loadjs.done('my-awesome-plugin');
});
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
$ ./node_modules/.bin/gulp examples:build
To view the examples you can use any static file server. To use the nodejs
http-server module:
$ npm install http-server
$ ./node_modules/.bin/http-server -p 3000
Then visit http://localhost:3000/examples
- Build distribution files
$ ./node_modules/.bin/gulp dist:build
The files will be located in the dist
directory.
Run tests
To run the browser tests first build the loadjs
library:
$ ./node_modules/.bin/gulp test:build
Then visit http://localhost:3000/test