Lazy Script Loading: Stop reptitive calls to JavaScirpts

Bob Matsuoka discussed how to lazily load Javascript script files by appending script elements to the HEAD tag in his article “Lazily load functionality via Unobtrusive Scripts.” A common use of a lazy loader is in conjunction with "require" type function that allows you to specify which scripts are needed for a particular script to execute. […]

Bob Matsuoka discussed how to lazily load Javascript script files by appending script elements to the HEAD tag in his article “Lazily load functionality via Unobtrusive Scripts.”

A common use of a lazy loader is in conjunction with "require" type function that allows you to specify which scripts are needed for a particular script to execute. Since library scripts are often called by more than one script, I thought it important to allow my lazy loader to track which scripts have already been loaded on a page to prevent unnecessary re-loading.

In this example, I've created the LazyLoader.scripts array. As each script is called, this script src is tested against the array of scripts already called, and if it exists, the script is not re-loaded, but its callback is executed. This allows you to call any required script as often as needed with impunity.

Full Article