Created
February 20, 2013 17:18
-
-
Save scottjehl/4997240 to your computer and use it in GitHub Desktop.
simple & qualified async script 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
<head> | |
.... | |
</head> | |
<script> | |
(function( w ){ | |
var doc = w.document, | |
// quick async script inject | |
ref = doc.getElementsByTagName( "script" )[0], | |
loadJS = function( src ){ | |
var elem = doc.createElement( "script" ); | |
elem.src = src; | |
ref.parentNode.insertBefore(elem, ref); | |
}; | |
// load a script | |
loadJS( "foo.js" ); | |
// or broadly qualify to newer browsers first: | |
if( "querySelector" in doc ){ | |
loadJS( "foo.js" ); | |
} | |
}( this )); | |
</script> | |
... | |
</head> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment