Test if jQuery is loaded

So to test if jQuery is loaded or not we can use 2 methods.

Method 1:
[javascript]
if (window.jQuery) {
// jQuery is loaded
} else {
// jQuery is not loaded
}
[/javascript]
Method 2:
[javascript]
if (typeof jQuery == ‘undefined’) {
// jQuery is not loaded
} else {
// jQuery is loaded
}
[/javascript]
Load jQuery:
[javascript]
if (!window.jQuery) {
var jq = document.createElement(‘script’); jq.type = ‘text/javascript’;
// Path to jquery.js file, eg. Google hosted version
jq.src = ‘/path-to-your/jquery.min.js’;
document.getElementsByTagName(‘head’)[0].appendChild(jq);
}
[/javascript]
NOTE:
Here we are checking for jQuery function being defined or not. This is a safe way to check for jQuery library being loaded. In case you are not using any other javascript libraries like prototype.js or mootools.js, then you can also check for $ instead of jQuery.