Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.