Jim_CS
Jim_CS

Reputation: 4172

How to determine if a web page is jquery-enabled?

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

Answers (8)

phucbm
phucbm

Reputation: 893

I would like to use typeof operator.

if (typeof jQuery !== 'undefined') {
    // jQuery IS loaded
}

Upvotes: 0

Arun Mishra
Arun Mishra

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

jimmyplaysdrums
jimmyplaysdrums

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

Rich
Rich

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.

enter image description here

Upvotes: 15

user1115253
user1115253

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

Matt Seymour
Matt Seymour

Reputation: 9395

do a check to see if the javascript object is initialised.

if(jQuery)
{
    alert('jquery active');
}

Upvotes: 2

JaredMcAteer
JaredMcAteer

Reputation: 22536

if (jQuery) { // yup it's there }

Upvotes: 0

Calvin Froedge
Calvin Froedge

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

Related Questions