Created
May 27, 2015 20:20
-
-
Save tigerhawkvok/93c842713e754d78740d to your computer and use it in GitHub Desktop.
Load Javascript file after page load
This file contains hidden or 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
loadJS = (src, callback = new Object(), doCallbackOnError = true) -> | |
### | |
# Load a new javascript file | |
# | |
# If it's already been loaded, jump straight to the callback | |
# | |
# @param string src The source URL of the file | |
# @param function callback Function to execute after the script has | |
# been loaded | |
# @param bool doCallbackOnError Should the callback be executed if | |
# loading the script produces an error? | |
### | |
if $("script[src='#{src}']").exists() | |
if typeof callback is "function" | |
try | |
callback() | |
catch e | |
console.error "Script is already loaded, but there was an error executing the callback function - #{e.message}" | |
# Whether or not there was a callback, end the script | |
return true | |
# Create a new DOM selement | |
s = document.createElement("script") | |
# Set all the attributes. We can be a bit redundant about this | |
s.setAttribute("src",src) | |
s.setAttribute("async","async") | |
s.setAttribute("type","text/javascript") | |
s.src = src | |
s.async = true | |
# Onload function | |
onLoadFunction = -> | |
state = s.readyState | |
try | |
if not callback.done and (not state or /loaded|complete/.test(state)) | |
callback.done = true | |
if typeof callback is "function" | |
try | |
callback() | |
catch e | |
console.error "Postload callback error - #{e.message}" | |
catch e | |
console.error "Onload error - #{e.message}" | |
# Error function | |
errorFunction = -> | |
console.warn "There may have been a problem loading #{src}" | |
try | |
unless callback.done | |
callback.done = true | |
if typeof callback is "function" and doCallbackOnError | |
try | |
callback() | |
catch e | |
console.error "Post error callback error - #{e.message}" | |
catch e | |
console.error "There was an error in the error handler! #{e.message}" | |
# Set the attributes | |
s.setAttribute("onload",onLoadFunction) | |
s.setAttribute("onreadystate",onLoadFunction) | |
s.setAttribute("onerror",errorFunction) | |
s.onload = s.onreadystate = onLoadFunction | |
s.onerror = errorFunction | |
document.getElementsByTagName('head')[0].appendChild(s) | |
true |
This file contains hidden or 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
loadJS = function(src, callback, doCallbackOnError) { | |
var e, errorFunction, onLoadFunction, s; | |
if (callback == null) { | |
callback = new Object(); | |
} | |
if (doCallbackOnError == null) { | |
doCallbackOnError = true; | |
} | |
/* | |
* Load a new javascript file | |
* | |
* If it's already been loaded, jump straight to the callback | |
* | |
* @param string src The source URL of the file | |
* @param function callback Function to execute after the script has | |
* been loaded | |
* @param bool doCallbackOnError Should the callback be executed if | |
* loading the script produces an error? | |
*/ | |
if ($("script[src='" + src + "']").exists()) { | |
if (typeof callback === "function") { | |
try { | |
callback(); | |
} catch (_error) { | |
e = _error; | |
console.error("Script is already loaded, but there was an error executing the callback function - " + e.message); | |
} | |
} | |
return true; | |
} | |
s = document.createElement("script"); | |
s.setAttribute("src", src); | |
s.setAttribute("async", "async"); | |
s.setAttribute("type", "text/javascript"); | |
s.src = src; | |
s.async = true; | |
onLoadFunction = function() { | |
var state; | |
state = s.readyState; | |
try { | |
if (!callback.done && (!state || /loaded|complete/.test(state))) { | |
callback.done = true; | |
if (typeof callback === "function") { | |
try { | |
return callback(); | |
} catch (_error) { | |
e = _error; | |
return console.error("Postload callback error - " + e.message); | |
} | |
} | |
} | |
} catch (_error) { | |
e = _error; | |
return console.error("Onload error - " + e.message); | |
} | |
}; | |
errorFunction = function() { | |
console.warn("There may have been a problem loading " + src); | |
try { | |
if (!callback.done) { | |
callback.done = true; | |
if (typeof callback === "function" && doCallbackOnError) { | |
try { | |
return callback(); | |
} catch (_error) { | |
e = _error; | |
return console.error("Post error callback error - " + e.message); | |
} | |
} | |
} | |
} catch (_error) { | |
e = _error; | |
return console.error("There was an error in the error handler! " + e.message); | |
} | |
}; | |
s.setAttribute("onload", onLoadFunction); | |
s.setAttribute("onreadystate", onLoadFunction); | |
s.setAttribute("onerror", errorFunction); | |
s.onload = s.onreadystate = onLoadFunction; | |
s.onerror = errorFunction; | |
document.getElementsByTagName('head')[0].appendChild(s); | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment