Skip to content

Instantly share code, notes, and snippets.

@mwurzberger
Last active August 29, 2015 14:07
Show Gist options
  • Save mwurzberger/01006630c4dd814b487d to your computer and use it in GitHub Desktop.
Save mwurzberger/01006630c4dd814b487d to your computer and use it in GitHub Desktop.
jQuery Load Script Function
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