Code: Lazy-load your JavaScript

Chapter 8 - Ajax Optimization

Many JavaScript libraries employ a form of “lazy-loading” or on-demand fetching by which code is loaded only when necessary. For example, you might employ this:

window.onload = function ( ) {
lazyCodeLoad(["http://example.com/somelib.js","http://example.com/otherlib.js"]) }

where the function defined reads each URI in the array and just creates a script tag using the DOM. It then sets its source to the file in question and inserts it into the page to retrieve it.