There are several examples of the concept of “lazy loading” out there. Dojo is probably the most well known, but the thing I don’t like about Dojo is that the initial include is so expensive (127K!!! - and that’s before you include any widgets).

While digging through ajaxpatterns.org I found this entry on On-Demand Javascript that outlines the concept really well, illustrates how one would implement it, and also points to a couple of implementatins already out there (including Dojo). It’s worth a quick read, especially as we start considering standardizing some of our code libraries.

How will the script determine which Javascript to download?

The script-downloading mechanism itself is quite easy, but determining when you need to download requires some consideration. Imagine you have a Fat Client numerical modelling website, with hundreds of algorithms available to be downloaded whenever the user requires them. Based on user input, how do you decide if you need to download something, and if so, what? Maintaining a long switch statement is going to get pretty tired - you ideally want a solution that’s as unobtrusive as possible. MapBuilder, discussed in the examples, for instance, integrates Javascript loading into the overall application configuration.

…more>>