Skip to content

Instantly share code, notes, and snippets.

@numist
Created December 5, 2011 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numist/1434854 to your computer and use it in GitHub Desktop.
Save numist/1434854 to your computer and use it in GitHub Desktop.
javascript resource loader
/*
* Copyright © 2011 by Scott Perry
* Released under the MIT License; its terms are at the end of this file.
*
* This file depends on:
* • jQuery (tested against v1.4.2)
* http://jquery.com/
*
* Pages that include this file should load all their javascript files (other
* than jQuery) by using jsLoad("path").
*
* Inspired by:
* http://code.google.com/speed/page-speed/docs/payload.html#DeferLoadingJS
*
******************************************************************************
*/
// call this to load/queue your file
function jsLoad(script) {
jsLoad_Scripts.push(script);
if(jsLoad_Locked == 0) {
jsLoad_Insert();
}
}
// pending scripts
var jsLoad_Scripts = [];
// loaded scripts
var jsLoad_Loaded = [];
// global lock
var jsLoad_Locked = -1;
// add/evaluate a script
function jsLoad_Insert() {
if(jsLoad_Scripts.length == 0) {
// first run? then we're done.
if(jsLoad_Locked == -1) {
jsLoad_Locked = 0;
}
return;
}
script = jsLoad_Scripts.shift();
// duplicate barrier
if(jsLoad_Loaded.indexOf(script) != -1) {
jsLoad_Insert();
return;
}
jsLoad_Loaded.push(script);
if(jsLoad_Locked == -1) {
// document just finished loading
var element = document.createElement("script");
element.charset = "utf-8";
element.type = "text/javascript";
element.src = script;
document.body.appendChild(element);
// recurse
jsLoad_Insert();
} else {
// document has finished loading, use serial XHRs to prevent a race
if(jsLoad_Locked != 0) {
return;
}
jsLoad_Locked = 1;
$.ajax({
type: "GET",
url: script,
dataType: "script",
success: function(data) {
// NOTE: script is automatically evaluated by jQuery
// unlock and handle the next script
jsLoad_Locked = 0;
jsLoad_Insert();
}
});
}
}
/*
* when the document is loaded, insert pending js
* NOTE: firing jsLoad_Insert for the first time after the document is ready can
* cause a race condition between scripts that depend on each other.
*/
if (window.addEventListener) {
window.addEventListener("load", jsLoad_Insert, false);
} else if (window.attachEvent) {
window.attachEvent("onload", jsLoad_Insert);
} else {
window.onload = jsLoad_Insert;
}
/*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
function jsLoad_Insert(){if(jsLoad_Scripts.length==0){if(jsLoad_Locked==-1){jsLoad_Locked=0}return}script=jsLoad_Scripts.shift();if(jsLoad_Loaded.indexOf(script)!=-1){jsLoad_Insert();return}jsLoad_Loaded.push(script);if(jsLoad_Locked==-1){var a=document.createElement("script");a.charset="utf-8";a.type="text/javascript";a.src=script;document.body.appendChild(a);jsLoad_Insert()}else{if(jsLoad_Locked!=0){return}jsLoad_Locked=1;$.ajax({type:"GET",url:script,dataType:"script",success:function(a){jsLoad_Locked=0;jsLoad_Insert()}})}}function jsLoad(a){jsLoad_Scripts.push(a);if(jsLoad_Locked==0){jsLoad_Insert()}}var jsLoad_Scripts=[];var jsLoad_Loaded=[];var jsLoad_Locked=-1;if(window.addEventListener){window.addEventListener("load",jsLoad_Insert,false)}else if(window.attachEvent){window.attachEvent("onload",jsLoad_Insert)}else{window.onload=jsLoad_Insert}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment