Last active
August 29, 2015 14:07
-
-
Save mwurzberger/01006630c4dd814b487d to your computer and use it in GitHub Desktop.
jQuery Load Script Function
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, callback) | |
{ | |
var head = document.getElementsByTagName("head")[0]; | |
var script = document.createElement("script"); | |
script.src = url; | |
// Attach handlers for all browsers | |
var done = false; | |
script.onload = script.onreadystatechange = function() | |
{ | |
if( !done && ( !this.readyState | |
|| this.readyState == "loaded" | |
|| this.readyState == "complete") ) | |
{ | |
done = true; | |
// Continue your code | |
callback(); | |
// Handle memory leak in IE | |
script.onload = script.onreadystatechange = null; | |
head.removeChild( script ); | |
} | |
}; | |
head.appendChild(script); | |
} | |
// Usage: | |
// This code loads jQuery and executes some code when jQuery is loaded | |
loadScript("http://code.jquery.com/jquery-latest.js", function() | |
{ | |
$('my_element').hide(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment