Created
January 3, 2010 05:33
-
-
Save why404/267841 to your computer and use it in GitHub Desktop.
loading javascript dynamically
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
| 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