LoadJS
LoadJS is a tiny async loader for modern browsers (899 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, 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.
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 899 bytes (minified + gzipped).
Here's an example of what you can do with LoadJS:
// define a dependency bundle and execute code when it loads; loadjs;
You can also use more advanced syntax for more options:
// define a dependency bundle with advanced options; loadjs;
The latest version of LoadJS can be found in the dist/
directory in this repository:
- https://cdn.rawgit.com/muicss/loadjs/4.2.0/dist/loadjs.js (for development)
- https://cdn.rawgit.com/muicss/loadjs/4.2.0/dist/loadjs.min.js (for production)
You can also use it as a CJS or AMD module:
$ npm install --save loadjs
var loadjs = ; ; loadjs;
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:
- Safari
- Chrome
Note: LoadJS treats empty CSS files as load failures in IE9-11 and uses rel="preload"
to load CSS files in Edge (to get around lack of support for onerror events on <link rel="stylesheet">
tags)
Documentation
-
Load a single file
; -
Fetch files in parallel and load them asynchronously
; -
Fetch JavaScript, CSS and image files
; -
Force treat file as CSS stylesheet
; -
Force treat file as image
; -
Add a bundle id
; -
Use .ready() to define bundles and callbacks separately
;loadjs; -
Use multiple bundles in .ready() dependency lists
;;loadjs; -
Chain .ready() together
;;loadjs; -
Use Promises to register callbacks
; -
Check if bundle has already been defined
if !loadjs; -
Fetch files in parallel and load them in series
; -
Add an error callback
; -
Retry files before calling the error callback
;// NOTE: Using `numRetries` with `async: false` can cause files to load out-of-sync on retries -
Execute a callback before script tags are embedded
; -
Bypass LoadJS default DOM insertion mechanism (DOM
<head>
); -
Use bundle ids in error callback
;;;// wait for multiple depdendenciesloadjs; -
Use .done() for more control
loadjs;{loadjs;}{loadjs;} -
Reset dependency trackers
loadjs; -
Implement a require-like dependency manager
var bundles ='bundleA': '/file1.js' '/file2.js''bundleB': '/file3.js' '/file4.js';{bundleIds;loadjs;};;;
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-examplesTo 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 3000Then visit http://localhost:3000/examples
-
Build distribution files
$ npm run build-distThe files will be located in the
dist
directory. -
Run tests
To run the browser tests first build the
loadjs
library:$ npm run build-testsThen visit http://localhost:3000/test
-
Build all files
$ npm run build-all