$script.js - Async JavaScript loader & dependency manager
$script.js
is an asynchronous JavaScript loader and dependency manager with an astonishingly impressive lightweight footprint. Like many other script loaders, $script.js allows you to load script resources on-demand from any URL and not block other resources from loading (like CSS and images). Furthermore, it's unique interface allows developers to work easily with even the most complicated dependencies, which can often be the case for large, complex web applications.
Browser Support
- IE 6+
- Opera 10+
- Safari 3+
- Chrome 1+
- Firefox 2+
Examples
old school - blocks CSS, Images, AND JS!
middle school - loads as non-blocking, but has multiple dependents
new school - loads as non-blocking, and ALL js files load asynchronously
// load jquery and plugin at the same time. name it 'bundle'// load your usage/*--- in my-jquery-plugin.js ---*/$script/*--- in my-app-that-uses-plugin.js ---*/$script
Exhaustive list of ways to use $script.js
$script// create an id and callback inline$scriptvar dependencyList =foo: 'foo.js'bar: 'bar.js'thunk: 'thunkor.js' 'thunky.js'// wait for multiple depdendencies!$script// in my-awesome-plugin.js$script// in index.html$script
$script.path()
Optionally to make working with large projects easier, there is a path variable you can set to set as a base.
$script;
Note that this will include all scripts from here on out with the base path. If you wish to circumvent this for any single script, you can simply call $script.get()
$script$script
$script.urlArgs()
As of 2.5.5 it's possible to concat URL arguments (i.e. a query string) to the script path. This is especially useful when you're in need of a cachebuster and works as follows:
$script;
Please note that Squid, a popular proxy, doesn’t cache resources with a querystring. This hurts performance when multiple users behind a proxy cache request the same file – rather than using the cached version everybody would have to send a request to the origin server. So ideally, as Steve Souders points out, you should rev the filename itself.
Developers
Building a $script environment works like this:
npm install -devmake
Ender support
You can add $script
to your existing ender build by adding it as such:
ender add scriptjs
Get access to it as such:
$
You can also require it as such:
var $S =