Skip to content

Instantly share code, notes, and snippets.

@notacouch
Created April 2, 2015 19:27
Show Gist options
  • Save notacouch/68e9ce446b8facf86788 to your computer and use it in GitHub Desktop.
Save notacouch/68e9ce446b8facf86788 to your computer and use it in GitHub Desktop.
Load jQuery 1.11.2 asynchronously, e.g. for IE8 in Rendera
// @link http://blog.kevinchisholm.com/asynchronous-javascript/cross-browser-asynchronous-javascript-script-loading/
function loadScript(url,callback){
if(!url || !(typeof url === 'string')){return};
var script = document.createElement('script');
//if this is IE8 and below, handle onload differently
if(typeof document.attachEvent === "object"){
script.onreadystatechange = function(){
//once the script is loaded, run the callback
if (script.readyState === 'loaded'){
if (callback){callback()};
};
};
} else {
//this is not IE8 and below, so we can actually use onload
script.onload = function(){
//once the script is loaded, run the callback
if (callback){callback()};
};
};
//create the script and add it to the DOM
script.src = url;
document.getElementsByTagName('head')[0].appendChild(script);
};
loadScript('//code.jquery.com/jquery-1.11.2.min.js', function(){
// code here
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment