How to Properly Test if jQuery is Loaded

I saw many people claiming you can test for the existence of jQuery like this:

1
2
3
4
5
if (jQuery) {
	// jQuery exists, put code here
} else {
	// jQuery not loaded, put code here
}

This is incorrect. It will work fine if jQuery is loaded, but if it's not loaded, javascript will throw an error and the code to execute if jQuery is not loaded will probably not be processed. The modification to fix this is simple:

1
2
3
4
5
if (window.jQuery) {
	// jQuery exists, put code here
} else {
	// jQuery not loaded, put code here
}
[ad]