using.js

Author: Jon Davis <jon@jondavis.net>
Current Version: 1.3.1
Released: April 17, 2008
Download from: http://cachefile.net/scripts/using/

The blog page that explains using.js is here: http://www.jondavis.net/blog/post/2008/04/Javascript-Introducing-Using-(js).aspx

This is a quick and dirty test of using.js. The using.js (10kb) script allows you specify a dependency script with its source URL for a particular feature so that it can be late-loaded, but it will not re-load the source twice. There is no dependency upon other script libraries like jQuery to get started with using.js.

The callback function will try to wait a bit for the referenced script to load. The callback is truly synchronous (no wait) or the delay is set using using.wait=[milliseconds];. You can reference scripts on different domains by setting the global using.wait property, or by declaring the URIs as external URIs by passing true and/or an optional post-load milliseconds parameter before each URL, i.e.
using.register(true, 750, 'http://cachefile.net/scripts/jquery/1.2.3/jquery-1.2.3.js');.

The URL parameter in using.register()can take several URLs, not just one, as additional (third, fourth, ..) parameters to the using.register() function.

You do not need a callback function when calling using() if you just want to make sure the referenced script is loaded. using('jquery'); works fine as long as it has been pre-registered.

The big advantage of using.js is that you can manage one source script that does nothing but register a big list of script files by name, then in a larger web client app you can reference using.js and the registrations script and then have all you need for your app.

Test: Click on the buttons below more than once:

invoke a function declared from helloworld.js
end result: 1st click, "Hello World! I'm loaded!!", 2nd click, "I'm already loaded."
code
fetch and invoke helloworld2.js
end result: 1st click only == alert from the script
code
fetch and load a function from an outside domain
end result: invoke moo tools function $random()
code
fetch and load variables from two different files under one declared 'registration'
end result: neither of two variables 'undefined'
code
fetch and load a library having a dependency upon another declared 'registration'
end result: "please wait"
code
basic test to be sure that the object (in this case a string) going in is also what comes out (a string, not the window [object])
code
declare multiple pre-declared dependencies for a callback function
end result: "please wait"
code
load and run a couple URLs without pre-registering
end result: "please wait"
code