On-Demand JavaScript Explained [con't]
Whereas the XMLHttpRequest technique allows you to download individual code
snippets, the following techniques are used to download an entire JavaScript
file at once. They also have the advantage that you can load JavaScript from
external domains and the script will be included in the browser cache.
Setting a Script's SRC
Property
A script element's src
property can be used to load a new script at any time.
Previously-loaded scripts remain in memory, even after the src
property has changed.
Here's how to do it:
Dynamically Creating the <SCRIPT> Element
This technique goes further by dynamically creating the element from scratch.
DOM methods are used to reference the HEAD
portion of the document, create a
new script element, set its type
and src
properties, and append it to the HEAD
element:
Writing a Script Element Using document.write()
document.write()
function. Although
easy to use, it does have a limitation in that it must be called during the
loading of the page; otherwise, it will clobber the existing document! You must
also pay attention to include the "/" escape character in the closing
<SCRIPT>
tag:
Here's a real life example of the above technique:
I recently worked on a chat application that was built using the Prototype Framework. My job was to make the chat app work within a content management system that used MooTools 1.1 or 1.2. To do that, I included a JavaScript file that would check for the presence of MooTools in memory and load it if it was not there. Subsequent code initialized functions that would bridge the gap between the two frameworks. The following code snippet is part of the main PHP file:
Here's a couple of the JavaScript functions that are referenced above.
The loadMootools()
function uses document.write()
to add the script to the page.
The initScripts()
function includes all the required functionality to delegate
Prototype methods to equivalent MooTools ones or to those from own library:
Script Loading Tools
There are already plenty of excellent libraries out there that utilize dynamic
script loading. Some are limited to their own modules, while others can be used
to import a variety of resources.
The Dojo JS Library
Dojo is one of the few JavaScript Frameworks that gives you the choice of which modules to include in your page.
The JavaScript Archive Network (JSAN)
JSAN may truly be ahead of their time by hosting an extensive library of JavaScript
modules. Perhaps following the JavaScript On-Demand concept to its conclusion
leads to the storing of scripts in online repositories so that developers can
access a multitude of code snippets for every usage. Their method of including
modules is called JSAN.use()
. Here's an example of a script that performs a
test using a JSAN library:
The Persevere Framework
Persevere is an open source set of tools for persistence and distributed computing
using the JSON interfaces of HTTP REST, JSON-RPC, JSONPath, and REST Channels.
Today, we touched on some basics of On-Demand JavaScript, but that is far from the end of the story. Even newer models are being brought into reality by ambitious Web developers and systems architects. For instance, a subcategory of On-Demand JavaScript is called Predictive Fetch. This strategy is to not only download code and other resources as required, but it has the browser anticipate likely user actions and call the server in preparation! This can be achieved using any number of clues, such as the user's profile and history. For instance, if the user always navigates to a certain page as soon as they log on, then the homepage could download that page's content immediately. It's also often possible to predict what the user will do next from their current activity. For example, if a user has just added an item to their shopping cart, they will likely be conducting a purchase in the not-too-distant future; developers could then consider downloading the customer's previous address details. All this may sound far fetched to you, but Google Maps already uses Predictive Fetching to download content that is just beyond the viewable map area, in anticipation of further navigation.
Original: March 18, 2009