Skip to content

Instantly share code, notes, and snippets.

@voodooGQ
Forked from travist/gist:2863998
Created June 3, 2012 16:18
Show Gist options
  • Save voodooGQ/2864056 to your computer and use it in GitHub Desktop.
Save voodooGQ/2864056 to your computer and use it in GitHub Desktop.
jQuery: Add jQuery Dynamically
var jQueryReady = function($) {
// Your code goes here...
console.log('jQuery Ready!');
};
// Make sure jQuery is ready.
if (typeof jQuery == 'undefined') {
var protocol = window.location.protocol;
if (protocol.charAt(protocol.length - 1) == ':') {
protocol = protocol.substring(0, protocol.length - 1);
}
var tag = document.createElement('script');
var src = protocol;
src += '://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
tag.src = src;
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
setTimeout(function tryAgain() {
if (typeof jQuery == 'undefined') {
setTimeout(tryAgain, 200);
}
else {
jQueryReady(jQuery);
}
}, 200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment