Skip to content

Instantly share code, notes, and snippets.

@jonahlyn
Created September 19, 2011 17:00
Show Gist options
  • Save jonahlyn/1226954 to your computer and use it in GitHub Desktop.
Save jonahlyn/1226954 to your computer and use it in GitHub Desktop.
Load jQuery only if it hasn't already been included somewhere on the page.
/* Load jQuery only if needed */
(function(){
var scriptAdded = false, js;
function initJQuery() {
if(typeof(jQuery) === 'undefined'){
if(!scriptAdded){
js = document.createElement('script');
js.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js';
var first = document.getElementsByTagName('script')[0];
first.parentNode.insertBefore(js, first);
scriptAdded = true;
}
setTimeout(initJQuery, 50);
} else {
// Do what you need to do with jQuery here.
}
}
initJQuery();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment