An extremely minimal asynchronous JavaScript loader, async-load has a full unit test suite that passes in all popular browsers (and some not so popular browsers including IE6+), is under 160bytes when compiled with Uglify2 and gzipped, and has simple but featured API.
This library is made to be as small as possible so it can be inlined into the head of your HTML. This means it can be used to bootstrap your application without using blocking script tags or ugly boilerplate code.
Three options are available for getting the source:
- Download the latest release.
- Clone the repo:
git clone git://github.com/heyday/async-load.git
. - Install with Bower:
bower install async-load
. - Install with npm:
npm install async-load
.
-
Configure your loader with a package:
packages: [ { name: 'async-load', location: 'path/to/async-load/', main: 'load' }, // ... other packages ... ]
-
define( [ 'async-load', ... ], function( load, ... ) { ... } );
orrequire( [ 'async-load', ... ], function( load, ... ) { ... } );
require( 'async-load' );
<script src="path/to/async-load/load.js"></script>
async-load
will be available aswindow.asyncLoad
asyncLoad( 'path/to/file.js', mySuccessOrErrorFunction, timeoutInMilliseconds );
define( [ 'async-load' ], function( asyncLoad ) {
asyncLoad( 'path/to/your/file.js' );
} );
var asyncLoad = require( 'async-load' );
asyncLoad( 'path/to/your/file.js', function() {
// Success or error callback
} );
window.asyncLoad( 'path/to/your/file.js' );
asyncLoad( 'path/to/your/file.js', function() {
// Success or error callback
} );
Due to IE not supporting the standard onload
and onerror
events, it is not possible to reliably determine the status of the loaded file (i.e., whether it was successful or there was an error). Because of this the async-load API has only a single callback, which is called in both instances. It is recommended, therefore, that you determine the status of the load within the callback function.
asyncLoad( 'path/to/your/file.js', function() {
try {
window.myInitFunction();
} catch( e ) {
console.log( 'ooo something went wrong, maybe the file didn\'t load?', e );
}
} );
asyncLoad( 'path/to/your/file.js', myCallBack, 1000 );
The time out will cause the callback function to be triggered after the timeout interval has elapsed. Be aware that this will not stop the file from eventually loading.
var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];
asyncLoad(('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js');
asyncLoad( '//use.typekit.com/{your-typekit-id}.js', function() {
try {
window.Typekit.load();
} catch(e) {}
} );
For more advanced loading of typekit that allows you to control the loading states see the typekit-load module.
npm install
- Install all required dev modulesnpm install -g grunt-cli
- Install Gruntgrunt test
- Lints all files, and then runs the unit tests in a PhantomJS instance
npm install
- Install all required dev modulesnpm install -g grunt-cli
- Install Gruntgrunt build
- Runs all tests, and then builds the production file