LoadJS

A tiny async loader / dependency manager for modern browsers (899 bytes)

README


LoadJS


LoadJS is a tiny async loader for modern browsers (961 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 theloadjs 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 961 bytes (minified + gzipped).

Here's an example of what you can do with LoadJS:

  1. ```html
  2. <script src="//unpkg.com/loadjs@latest/dist/loadjs.min.js"></script>
  3. <script>
  4.   // define a dependency bundle and execute code when it loads
  5.   loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar');
  6.   loadjs.ready('foobar', function() {
  7.     /* foo.js & bar.js loaded */
  8.   });
  9. </script>
  10. ```

You can also use more advanced syntax for more options:
  1. ```html
  2. <script src="//unpkg.com/loadjs@latest/dist/loadjs.min.js"></script>
  3. <script>
  4.   // define a dependency bundle with advanced options
  5.   loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', {
  6.     before: function(path, scriptEl) { /* execute code before fetch */ },
  7.     async: true,  // load files synchronously or asynchronously (default: true)
  8.     numRetries: 3  // see caveats about using numRetries with async:false (default: 0),
  9.     returnPromise: false  // return Promise object (default: false)
  10.   });
  11.   loadjs.ready('foobar', {
  12.     success: function() { /* foo.js & bar.js loaded */ },
  13.     error: function(depsNotFound) { /* foobar bundle load failed */ },
  14.   });
  15. </script>  
  16. ```

The latest version of LoadJS can be found in the dist/ directory in this repository:

It's also available from these public CDNs:

  UNPKG
  CDNJS

You can also use it as a CJS or AMD module:

  1. ```bash
  2. $ npm install --save loadjs
  3. ```

  1. ```javascript
  2. var loadjs = require('loadjs');

  3. loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar');

  4. loadjs.ready('foobar', function() {
  5.   /* foo.js & bar.js loaded */
  6. });
  7. ```

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 `` tags)