Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirontoli/2926331 to your computer and use it in GitHub Desktop.
Save mirontoli/2926331 to your computer and use it in GitHub Desktop.
Conditionally Load jQuery
//Conditionally load jQuery
//inspired by http://www.smashingmagazine.com/2010/05/23/make-your-own-bookmarklets-with-jquery/
function myOnLoadEvent() {
jQuery(document).ready(function($) {
alert('your code here');
});
}
function load_jQuery_conditionaly() {
if (typeof jQuery == 'undefined') {
var jQ = document.createElement('script');
jQ.type = 'text/javascript';
jQ.onload = jQ.onreadystatechange = myOnLoadEvent;
//use protocol relative url
jQ.src = '//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
document.body.appendChild(jQ);
}
else {
myOnLoadEvent();
}
};
//http://stackoverflow.com/questions/9434/how-do-i-add-an-additional-window-onload-event-in-javascript/688199#688199
if (window.addEventListener) // W3C standard
{
window.addEventListener('load', load_jQuery_conditionaly, false); // NB **not** 'onload'
}
else if (window.attachEvent) // Microsoft
{
window.attachEvent('onload', load_jQuery_conditionaly);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment