Supplementary

Three Functions for Loading Functions, Object Definitions, and CSS Rules Dynamically

Typically, web pages define JavaScript functions and objects, as well as CSS style-rules statically, by means of script and link elements within the head element. However, it is also possible to load such code dynamically, such that applications import functionality only when required.

This approach is far more efficient, as it consumes bandwidth and platform resources only when necessary, and this section presents a library of three small functions that automate the techniques involved.

Critically, these functions can be used in conjunction with AspectJS, and this is significant because the intersection of the technologies permits code downloads on demand but entirely transparently at the point of need. This is explored in the AspectJS Applications page entitled On-Demand Loading, and in the article entitled Transparency on Demand.

The functions that comprise the library are as follows:

  • loadJS_XHR - loads JavaScript functions and object definitions using XMLHTTPRequest
  • loadJS_STH - loads JavaScript object and function definitions dynamically using the 'Script-Tag Hack'
  • loadCSS - similar to loadJS_STH in that it loads style-sheet rule-sets using a variant of the script-tag hack

Note that, unlike the other libraries available on this site, there are no 'DbC' adjunct-functions for these resources currently (for the purposes of performing argument type, number and value checking), although this may be addressed at some point.

Tutorials

Using loadJS_XHR
Passing an Error Handler
Server/Network Failure

Using loadJS_STH
Race Conditions
Using an EventMarshaller

Using loadCSS

APIs

Functions
Signatures
Arguments
Returns
Exceptions