Skip to content

Instantly share code, notes, and snippets.

@why404
Created January 3, 2010 05:33
Show Gist options
  • Select an option

  • Save why404/267841 to your computer and use it in GitHub Desktop.

Select an option

Save why404/267841 to your computer and use it in GitHub Desktop.
loading javascript dynamically
function loadScript(url, func) {
var script = document.createElement("script");
if (script.addEventListener) {
script.addEventListener("load", func, false);
} else if (script.attachEvent) {
script.attachEvent("onreadystatechange", function() {
var target = window.event.srcElement;
if ((target.readyState == "loaded") || (target.readyState == "complete")) {
func();
}
});
}
script.setAttribute("type", "text/javascript");
script.setAttribute("src", url);
document.getElementsByTagName("head")[0].appendChild(script);
}
// There is also a Script loader plugin for jQuery, see http://code.google.com/p/jquery-loadscript/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment