Loading And Blocking JavaScript

LABjs  is a simple little tool (4.5k minified, 2.1k gzip’d!) for being able to load javascript files dynamically. It’s like many script loaders where the goal is to improve the speed of page load by allowing scripts to load in parallel. The thing it does differently than most others is it allows you to express via the API calls when it is necessary to preserve execution order for dependencies. By default, all scripts will still load in parallel, but execution order will be deferred until the proper time, if necessary.

What most loaders fail to do well (easily) is let you define dependencies simply based on loading order and the API calls. With regular script tags, the browser blocks for you, so you know for instance that jquery.js executes before jquery.ui.js.

Imagine this scenario: 3 scripts that can be executed in parallel (not dependent on each other), and then two more that need to wait for those 3 to load before executing. You can’t do that with script tags, and you also can’t do that very easily with most of the script loaders/frameworks out there.

LABjs lets you load pretty much any script file, whether you control it or not, with no intrusion or convention for dependencies, other than the order and blocking that you define. It keeps track of what you’ve asked for and what has downloaded, and lets you only define a handler once for a group of scripts that will execute together in parallel. The API style (with chaining) makes is very easy to convert a set of script tags in your page into code to load them, without having to worry that race conditions will cause issues for scripts loading in the wrong order if there are explicit dependencies involved.

Loading And Blocking JavaScript

Loading And Blocking JavaScript

Resource: Sample | Tutorial