Created
July 18, 2013 10:22
-
-
Save jeffery/6028290 to your computer and use it in GitHub Desktop.
add_dom_javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add_dom_javascript(scriptFile, callBackFunction, callBackParams) | |
{ | |
var fileAdded = false; | |
var script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.src = scriptFile; | |
if ( (callBackFunction) && (arguments.length >= 2) ) | |
{ | |
// loop through the addDomCallBacks array and determine if this file is already mentioned | |
for ( var i = 0; i < addDomCallBacks.length; i++ ) | |
{ | |
if ( addDomCallBacks[i].scriptSrc == scriptFile ) | |
{ | |
fileAdded = true; | |
// if the file is already loaded, just run the callback function | |
if ( addDomCallBacks[i].loaded == true ) | |
{ | |
callBackFunction( callBackParams ); | |
} | |
// otherwise add the callback to the array of callbacks | |
else | |
{ | |
addDomCallBacks[i].callBackFunctions.push( callBackFunction ); | |
addDomCallBacks[i].callBackParams.push( callBackParams ); | |
} | |
} | |
} | |
if ( fileAdded == false ) | |
{ | |
// create an object referencing the callback function | |
var callBackRef = {}; | |
callBackRef.scriptSrc = scriptFile; | |
callBackRef.callBackFunctions = Array(); | |
callBackRef.callBackFunctions.push( callBackFunction ); | |
callBackRef.callBackParams = Array(); | |
callBackRef.callBackParams.push( callBackParams ); | |
callBackRef.loaded = false; | |
addDomCallBacks.push(callBackRef); | |
var evl = {}; | |
evl.handleEvent = function(e) { | |
addJavascriptCallBack( scriptFile ); | |
}; | |
if ( script.addEventListener ) | |
{ | |
script.addEventListener('load' ,evl ,true); | |
} | |
else // For IE | |
{ | |
script.onreadystatechange = function() | |
{ | |
if (this.readyState == "loaded" || this.readyState == "complete") | |
{ | |
addJavascriptCallBack.call( this, scriptFile ); | |
} | |
} | |
} | |
} | |
} | |
if ( fileAdded == false ) | |
{ | |
document.getElementsByTagName('head')[0].appendChild(script); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment