Reputation: 4172
What is the best way to determine if a webpage is jquery enabled? Using jquery itself if that's the best way to determine it.
Upvotes: 16
Views: 23842
Reputation: 893
I would like to use typeof
operator.
if (typeof jQuery !== 'undefined') {
// jQuery IS loaded
}
Upvotes: 0
Reputation: 1
Well we have the working answers available.. Though for some lazy people that want even smart answer I would like to add : Simply try hitting
$
key word in the console of browser if (jQuery is loaded) then you would get
ƒ (e,t){return new E.fn.init(e,t)}
which is the minified jQuery library else it would throw reference error
ReferenceError: $ is not defined.
Upvotes: 0
Reputation: 249
Run this in the console:
if (window.jQuery) {
console.log("Yes there's jQuery!");
} else {
console.log("Nope, it's not on this site...");
};
Upvotes: 9
Reputation: 5721
The best way to check if jQuery is loaded is
if (window.jQuery) {
// jQuery is loaded
} else {
// jQuery is not loaded
}
If you check using if(jQuery){}
, and it isn't there, you are going to get a reference error like below, and it will break the execution of your script. By checking if the window object has a property called jQuery, if it isn't there, it will simply return undefined.
Upvotes: 15
Reputation:
Check in javascript if the jquery object exists.
Use the below if condition to check if jQuery object exists.
if(jQuery)
{
//jquery object exists
}
Upvotes: 2
Reputation: 9395
do a check to see if the javascript object is initialised.
if(jQuery)
{
alert('jquery active');
}
Upvotes: 2
Reputation: 16373
if(jQuery) //jquery object exists
jQuery isn't magic - it's essentially just a big object. You can check for it like you would any other object.
Same thing to ensure libraries within jQuery are loaded:
if(jQuery.DatePicker) //lib exists
Upvotes: 18