-
-
Save mirontoli/2926331 to your computer and use it in GitHub Desktop.
Conditionally Load jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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