Created
September 14, 2012 16:37
-
-
Save gustavopaes/3723133 to your computer and use it in GitHub Desktop.
load a script, like a jsonp file, with callback and scope support
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
/** | |
* Carrega um script e executa callback | |
*/ | |
(function(window, document, head) { | |
if(!window.loadScript) { | |
window.loadScript = function(url, callback, scope, charset) { | |
var sc = document.createElement('script'); | |
sc.type = 'text/javascript'; | |
sc.async = true; | |
sc.charset = charset || "utf-8"; | |
sc.src = url; | |
sc.onload = sc.onreadystatechange = (function(url, callback, scope, sc){ | |
return function(){ | |
if (!sc.readyState || sc.readyState == "loaded" || sc.readyState == "complete") { | |
head.removeChild(sc); | |
sc.onload = sc.onreadystatechange = null; | |
if(typeof callback == "function") { | |
callback.call(scope || null); | |
} | |
} | |
} | |
})(url, callback, scope, sc); | |
head.insertBefore(sc, head.firstChild); | |
return {"o":sc}; | |
} | |
} | |
}(window, document, document.getElementsByTagName("head")[0])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment