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:
- ```html
- <script src="//unpkg.com/loadjs@latest/dist/loadjs.min.js"></script>
- <script>
- // define a dependency bundle and execute code when it loads
- loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar');
- loadjs.ready('foobar', function() {
- /* foo.js & bar.js loaded */
- });
- </script>
- ```
You can also use more advanced syntax for more options:
- ```html
- <script src="//unpkg.com/loadjs@latest/dist/loadjs.min.js"></script>
- <script>
- // define a dependency bundle with advanced options
- loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', {
- before: function(path, scriptEl) { /* execute code before fetch */ },
- async: true, // load files synchronously or asynchronously (default: true)
- numRetries: 3 // see caveats about using numRetries with async:false (default: 0),
- returnPromise: false // return Promise object (default: false)
- });
- loadjs.ready('foobar', {
- success: function() { /* foo.js & bar.js loaded */ },
- error: function(depsNotFound) { /* foobar bundle load failed */ },
- });
- </script>
- ```
The latest version of LoadJS can be found in the dist/ directory in this repository:
https://cdn.rawgit.com/kubetail-org/loadjs/4.3.0/dist/loadjs.js (for development)
https://cdn.rawgit.com/kubetail-org/loadjs/4.3.0/dist/loadjs.min.js (for production)
It's also available from these public CDNs:
UNPKG
https://unpkg.com/loadjs@4.3.0/dist/loadjs.js (for development)
https://unpkg.com/loadjs@4.3.0/dist/loadjs.min.js (for production)
CDNJS
https://cdnjs.cloudflare.com/ajax/libs/loadjs/4.3.0/loadjs.js (for development)
https://cdnjs.cloudflare.com/ajax/libs/loadjs/4.3.0/loadjs.min.js (for production)
You can also use it as a CJS or AMD module:
- ```bash
- $ npm install --save loadjs
- ```
- ```javascript
- var loadjs = require('loadjs');
- loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar');
- loadjs.ready('foobar', function() {
- /* foo.js & bar.js loaded */
- });
- ```
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