Skip to content

Instantly share code, notes, and snippets.

@benbalter
Created April 4, 2011 18:05
Show Gist options
  • Save benbalter/902090 to your computer and use it in GitHub Desktop.
Save benbalter/902090 to your computer and use it in GitHub Desktop.
Conditionally Load jQuery
// Conditionally Load jQuery (javascript)
// Source: https://gist.github.com/gists/902090/
var init, maybeLoadJq;
init = function() {
jQuery(document).ready(function() {
alert('Your Code Here');
});
};
maybeLoadJq = function() {
var jQ;
if (!(typeof jQuery !== "undefined" && jQuery !== null)) {
jQ = document.createElement('script');
jQ.type = 'text/javascript';
jQ.onload = jQ.onreadystatechange = init;
jQ.src = '//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
return document.body.appendChild(jQ);
} else {
return init();
}
};
if (window.addEventListener) {
window.addEventListener('load', maybeLoadJq, false);
} else if (window.attachEvent) {
window.attachEvent('onload', maybeLoadJq);
}
#Conditionally load jQuery (Coffeescript)
#Source: https://gist.github.com/gists/902090/
#Inspired by: http://bit.ly/cFPMER
#function to fire once jQuery is loaded
init = ->
jQuery(document).ready ->
alert( 'Your Code Here')
#conditionally load jQuery
maybeLoadJq = ->
if !jQuery?
jQ = document.createElement 'script'
jQ.type = 'text/javascript'
jQ.onload = jQ.onreadystatechange = init
jQ.src = '//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
document.body.appendChild(jQ);
else
init()
# Source: http://bit.ly/TWwg2z
if window.addEventListener #W3C
window.addEventListener 'load', maybeLoadJq, false
else if window.attachEvent #IE
window.attachEvent 'onload', maybeLoadJq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment