Script.js - Asynchronous JavaScript Loader
$script.js is a compact, asynchronous JavaScript loader and dependency manager designed for browser environments. Its primary function is to load JavaScript resources on demand without blocking the rendering of other assets like CSS and images, a common issue with traditional `<script>` tags. The library, currently at version 2.5.9, focuses on simplifying dependency management for complex web applications by allowing scripts to be loaded in parallel and executed once their dependencies are met, using a unique `ready` callback mechanism. It differentiates itself through its lightweight footprint and its ability to manage intricate load sequences using named bundles or individual script IDs, making it suitable for older browser environments (IE6+, Opera10+). However, its release cadence appears to be very slow, with the last known stable version 2.5.9 released quite some time ago, indicating a maintenance or effectively abandoned status given the prevalence of modern bundlers.
Common errors
-
Uncaught ReferenceError: $script is not defined
cause The script.js library file has not been loaded into the page before your application code attempts to use the `$script` global.fixEnsure that `<script src="path/to/script.js"></script>` is included in your HTML before any custom JavaScript code that calls `$script()` methods. -
TypeError: Cannot read properties of undefined (reading 'ready')
cause Similar to the `ReferenceError`, this occurs when the `$script` object is not available or fully initialized when its methods like `ready` are called.fixVerify the correct loading order and ensure the `script.js` file path is accurate. If dynamically loading `script.js` itself, ensure its load callback is fired before attempting to use `$script` methods. -
The script at 'http://example.com/missing.js' failed to load.
cause A script specified in `$script()` or `$script.get()` could not be fetched due to a wrong URL, network issue, or server error.fixCheck the script URL for typos. Verify the script exists at the specified path. Use the optional error callback in `$script.ready()` to handle cases where dependencies are not found: `$script.ready(['dep'], function(){}, function(depsNotFound){ /* handle missing deps */ });`
Warnings
- breaking This library is primarily designed for older browser environments and traditional server-rendered applications. It does not integrate well with modern JavaScript module systems (ES Modules, CommonJS) or bundlers (Webpack, Rollup, Vite) and might cause unexpected behavior or global scope pollution when used alongside them.
- gotcha All scripts loaded via `$script.js` execute in the global scope. Variables and functions defined in these scripts will become global properties unless explicitly wrapped in IIFEs or closures, leading to potential naming conflicts.
- deprecated The `script.js` project appears to be largely unmaintained. While functional for its intended use case, it has not seen significant updates or new feature development for several years. This means it may not address modern web standards, security concerns, or performance optimizations.
- gotcha The `$script.urlArgs()` feature, while useful for cache busting, appends a query string to *all* subsequent script loads. This can prevent proxy caches (like Squid) from caching resources, potentially impacting performance for users behind such proxies.
Install
-
npm install scriptjs -
yarn add scriptjs -
pnpm add scriptjs
Imports
- $script
import $script from 'script.js'
<!-- In HTML --> <script src="script.js"></script> // In JavaScript, after loading $script('script.js', callback) - $script.ready
import { ready } from '$script';$script.ready('bundleName', function() { /* code */ }); - $script.path
$script().path('/js/modules/');$script.path('/js/modules/');
Quickstart
$script.path('/assets/js/');
// Load jQuery and a jQuery plugin concurrently, assigning them to a 'vendor' bundle ID
$script(['jquery-3.x.min.js', 'my-jquery-plugin.js'], 'vendor', function() {
console.log('jQuery and plugin loaded. Initializing application...');
});
// Load a core application module separately
$script('app-core.js', 'appCore');
// Wait for both the 'vendor' bundle and 'appCore' module to be ready
$script.ready(['vendor', 'appCore'], function() {
console.log('All core scripts are loaded and ready.');
// Application initialization code that depends on all these scripts
// For example, if jQuery is globally available via `window.$`
if (typeof window.$ !== 'undefined') {
console.log('jQuery version:', $.fn.jquery);
}
// Your application's main entry point
// myApp.init();
}, function(depsNotFound) {
console.error('Some dependencies were not found:', depsNotFound);
// Optionally, try to lazy load missing dependencies
});