Last active
November 3, 2020 21:39
-
-
Save vishaltelangre/7499430 to your computer and use it in GitHub Desktop.
load script async #js
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
function loadScript( url, callback ) { | |
// create script element | |
var script = document.createElement('script'); | |
script.async = true; | |
script.src = url; | |
// attach it to DOM | |
var entry = document.getElementsByTagName('script')[0]; | |
entry.parentNode.insertBefore(script, entry); | |
// execute callback after script is loaded | |
script.onload = script.onreadystatechange = function() { | |
var readyState = script.readyState; | |
if ( !readyState || /complete|loaded/.test( script.readyState ) ) { | |
if ( typeof callback === "function" ) callback(); | |
// detach event handler to avoid memory leaks in IE | |
script.onload = null; | |
script.onreadystatechange = null; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment