Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctbarna/918065 to your computer and use it in GitHub Desktop.
Save ctbarna/918065 to your computer and use it in GitHub Desktop.
Conditionally load jQuery (with slight version independence). Not expecting the API to change a whole lot in the sub-versions.
//Conditionally load jQuery
//inspired by http://www.smashingmagazine.com/2010/05/23/make-your-own-bookmarklets-with-jquery/
window.onload = function () {
if (typeof jQuery == 'undefined') {
var jQ = document.createElement('script');
jQ.type = 'text/javascript';
jQ.onload = jQ.onreadystatechange = myOnLoadEvent;
jQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js';
document.body.appendChild(jQ);
} else {
myOnLoadEvent();
}
};
function myOnLoadEvent() {
jQuery(document).ready(function($) {
alert('your code here');
});
}
@ctbarna
Copy link
Author

ctbarna commented Apr 13, 2011

jQ.onload was not referencing a real function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment