
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
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.
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); } });
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 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 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 is a tiny async loader for modern browsers (961 bytes).
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 961 bytes (minified + gzipped).
Here's an example of what you can do with LoadJS:
<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:
<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:
It's also available from these public CDNs:
You can also use it as a CJS or AMD module:
$ npm install --save loadjs
var loadjs = require('loadjs');
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar');
loadjs.ready('foobar', function() {
/* foo.js & bar.js loaded */
});
async: false
support only works in IE10+)LoadJS also detects script load failures from AdBlock Plus and Ghostery in:
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)
Load a single file
loadjs('/path/to/foo.js', function() {
/* foo.js loaded */
});
Fetch files in parallel and load them asynchronously
loadjs(['/path/to/foo.js', '/path/to/bar.js'], function() {
/* foo.js and bar.js loaded */
});
Fetch JavaScript, CSS and image files
loadjs(['/path/to/foo.css', '/path/to/bar.png', 'path/to/thunk.js'], function() {
/* foo.css, bar.png and thunk.js loaded */
});
Force treat file as CSS stylesheet
loadjs(['css!/path/to/cssfile.custom'], function() {
/* cssfile.custom loaded as stylesheet */
});
Force treat file as image
loadjs(['img!/path/to/image.custom'], function() {
/* image.custom loaded */
});
Load JavaScript files as modules with non-module fallbacks (in browsers without module support)
loadjs(['module!/path/to/foo.js', 'nomodule!/path/to/bar.js'], function() {
/* foo.js loaded with type="module" in browsers with module support, skipped silently in browsers without */
/* bar.js loaded with type="text/javascript" in browsers without module support, skipped silently in browsers with */
});
Add a bundle id
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', function() {
/* foo.js & bar.js loaded */
});
Use .ready() to define bundles and callbacks separately
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar');
loadjs.ready('foobar', function() {
/* foo.js & bar.js loaded */
});
Use multiple bundles in .ready() dependency lists
loadjs('/path/to/foo.js', 'foo');
loadjs(['/path/to/bar1.js', '/path/to/bar2.js'], 'bar');
loadjs.ready(['foo', 'bar'], function() {
/* foo.js & bar1.js & bar2.js loaded */
});
Chain .ready() together
loadjs('/path/to/foo.js', 'foo');
loadjs('/path/to/bar.js', 'bar');
loadjs
.ready('foo', function() {
/* foo.js loaded */
})
.ready('bar', function() {
/* bar.js loaded */
});
Use Promises to register callbacks
loadjs(['/path/to/foo.js', '/path/to/bar.js'], {returnPromise: true})
.then(function() { /* foo.js & bar.js loaded */ })
.catch(function(pathsNotFound) { /* at least one didn't load */ });
Check if bundle has already been defined
if (!loadjs.isDefined('foobar')) {
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', function() {
/* foo.js & bar.js loaded */
});
}
Fetch files in parallel and load them in series
loadjs(['/path/to/foo.js', '/path/to/bar.js'], {
success: function() { /* foo.js and bar.js loaded in series */ },
async: false
});
Add an error callback
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', {
success: function() { /* foo.js & bar.js loaded */ },
error: function(pathsNotFound) { /* at least one path didn't load */ }
});
Retry files before calling the error callback
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', {
success: function() { /* foo.js & bar.js loaded */ },
error: function(pathsNotFound) { /* at least one path didn't load */ },
numRetries: 3
});
// NOTE: Using `numRetries` with `async: false` can cause files to load out-of-sync on retries
Execute a callback before script tags are embedded
loadjs(['/path/to/foo.js', '/path/to/bar.js'], {
success: function() {},
error: function(pathsNotFound) {},
before: function(path, scriptEl) {
/* called for each script node before being embedded */
if (path === '/path/to/foo.js') scriptEl.crossOrigin = true;
}
});
Bypass LoadJS default DOM insertion mechanism (DOM <head>
)
loadjs(['/path/to/foo.js'], {
success: function() {},
error: function(pathsNotFound) {},
before: function(path, scriptEl) {
document.body.appendChild(scriptEl);
/* return `false` to bypass default DOM insertion mechanism */
return false;
}
});
Use bundle ids in error callback
loadjs('/path/to/foo.js', 'foo');
loadjs('/path/to/bar.js', 'bar');
loadjs(['/path/to/thunkor.js', '/path/to/thunky.js'], 'thunk');
// wait for multiple depdendencies
loadjs.ready(['foo', 'bar', 'thunk'], {
success: function() {
// foo.js & bar.js & thunkor.js & thunky.js loaded
},
error: function(depsNotFound) {
if (depsNotFound.indexOf('foo') > -1) {}; // foo failed
if (depsNotFound.indexOf('bar') > -1) {}; // bar failed
if (depsNotFound.indexOf('thunk') > -1) {}; // thunk failed
}
});
Use .done() for more control
loadjs.ready(['dependency1', 'dependency2'], function() {
/* run code after dependencies have been met */
});
function fn1() {
loadjs.done('dependency1');
}
function fn2() {
loadjs.done('dependency2');
}
Reset dependency trackers
loadjs.reset();
Implement a require-like dependency manager
var bundles = {
'bundleA': ['/file1.js', '/file2.js'],
'bundleB': ['/file3.js', '/file4.js']
};
function require(bundleIds, callbackFn) {
bundleIds.forEach(function(bundleId) {
if (!loadjs.isDefined(bundleId)) loadjs(bundles[bundleId], bundleId);
});
loadjs.ready(bundleIds, callbackFn);
}
require(['bundleA'], function() { /* bundleA loaded */ });
require(['bundleB'], function() { /* bundleB loaded */ });
require(['bundleA', 'bundleB'], function() { /* bundleA and bundleB loaded */ });
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
Install dependencies
Clone repository
$ git clone git@github.com:kubetail-org/loadjs.git
$ cd loadjs
Install node dependencies using npm
$ npm install
Build examples
$ npm run build-examples
To 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 3000
Then visit http://localhost:3000/examples
Build distribution files
$ npm run build-dist
The files will be located in the dist
directory.
Run tests
To run the browser tests first build the loadjs
library:
$ npm run build-tests
Then visit http://localhost:3000/test
Build all files
$ npm run build-all
4.3.0 - April, 11, 2024
FAQs
Tiny async loader for modern browsers
The npm package loadjs receives a total of 244,954 weekly downloads. As such, loadjs popularity was classified as popular.
We found that loadjs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.Ā It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600Ć faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.