Created
April 13, 2010 15:50
-
-
Save samuelcole/364746 to your computer and use it in GitHub Desktop.
zero dependency $.getScript for loading large external javascripts
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
| // zero dependency $.getScript for loading external javascript (like jQuery.js | |
| // or other big libraries) in a way that does not block the normal page load | |
| // (http://www.yuiblog.com/blog/2008/07/22/non-blocking-scripts/). | |
| // | |
| // heavily influenced by/copied from jQuery's ajax functions (http://jquery.com) | |
| window.$ = { | |
| getScript: function(script_src, callback) { | |
| var done = false; | |
| var head = document.getElementsByTagName("head")[0] || document.documentElement; | |
| var script = document.createElement("script"); | |
| script.src = script_src; | |
| script.onload = script.onreadystatechange = function() { | |
| if ( !done && (!this.readyState || | |
| this.readyState === "loaded" || this.readyState === "complete") ) { | |
| if(callback) callback(); | |
| // Handle memory leak in IE | |
| script.onload = script.onreadystatechange = null; | |
| if ( head && script.parentNode ) { | |
| head.removeChild( script ); | |
| } | |
| done = true; | |
| } | |
| }; | |
| head.insertBefore( script, head.firstChild ); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment