554

How do I check which version of jQuery is loaded on the client machine? The client may have jQuery loaded but I don't know how to check it. If they have it loaded how do I check the version and the prefix such as:

$('.class')
JQuery('.class')
Travis
  • 13,311
  • 4
  • 26
  • 40
Luke101
  • 63,072
  • 85
  • 231
  • 359
  • 2
    The client may have jQuery loaded if you included it as a file to download. What is the problem, exactly? – Jared Farrish Aug 07 '11 at 16:11
  • 1
    Dunno about what the OP needs it for, but if you're making a script or plugin that requires jQuery you could check and throw an error if it's not there or a too old version. – JJJ Aug 07 '11 at 16:22
  • 1
    possible duplicate of [Get jQuery version from inspecting the jQuery object](http://stackoverflow.com/questions/6867040/get-jquery-version-from-inspecting-the-jquery-object) – Mołot Jun 23 '15 at 13:13

13 Answers13

1029
if (typeof jQuery != 'undefined') {  
    // jQuery is loaded => print the version
    alert(jQuery.fn.jquery);
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 39
    There are 2 ways to check currently loaded jquery version: `jQuery.fn.jquery` and `jQuery().jquery` (shorthands: `$.fn.jquery` `$().jquery`). I wrote about [**it in details**](http://jquery-howto.blogspot.com/2009/02/how-to-check-jquery-version.html) with links to jquery source code if you'd like to dig deeper. – Uzbekjon Aug 16 '13 at 09:40
  • 3
    even simpler version `if (window.jQuery) { // jQuery is loaded => print the version alert(jQuery.fn.jquery); }` – asherrard Mar 14 '16 at 18:33
  • 8
    The check for undefined should be `!==` – plankguy Jan 27 '17 at 21:47
  • 1
    Note lowercase :) – Nate Anderson Jul 25 '17 at 15:36
  • 5
    Don't use `jQuery().jquery`. It creates a jQuery object for nothing, just use what the answer says. –  Aug 02 '18 at 22:32
  • This answer will do the job and is a good answer. However, `$.fn.jquery` or `jQuery.fn.jquery` will get the same result just in the console and not as an alert; no need to overcomplicate it. Also I'd go with the comparison as `if (typeof jQuery !== undefined) { ... }` – Rockin4Life33 Oct 04 '18 at 17:07
41

My goto means to determine the version:

$.fn.jquery

Another similar option:

$().jQuery

If there is concern that there may be multiple implementations of $ — making $. ambiguous — then use jQuery instead:

jQuery.fn.jquery

Recently I have had issues using $.fn.jquery and $().jQuery on a few sites so I wanted to note a third simple command to pull the jQuery version.

If you get back a version number — usually as a string — then jQuery is loaded and that is what version you're working with. If not loaded then you should get back undefined or maybe even an error.

Pretty old question and I've seen a few people that have already mentioned my answer in comments. However, I find that sometimes great answers that are left as comments can go unnoticed; especially when there are a lot of comments to an answer you may find yourself digging through piles of them looking for a gem. Hopefully this helps someone out!

Rockin4Life33
  • 2,240
  • 1
  • 31
  • 29
37

You can just check if the jQuery object exists:

if( typeof jQuery !== 'undefined' ) ... // jQuery loaded

jQuery().jquery has the version number.

As for the prefix, jQuery should always work. If you want to use $ you can wrap your code to a function and pass jQuery to it as the parameter:

(function( $ ) {
    $( '.class' ).doSomething();  // works always
})( jQuery )
JJJ
  • 32,902
  • 20
  • 89
  • 102
19

…just because this method hasn't been mentioned so far - open the console and type:

$ === jQuery

As @Juhana mentioned above $().jquery will return the version number.

ptim
  • 14,902
  • 10
  • 83
  • 103
17

I have found this to be the shortest and simplest way to check if jQuery is loaded:

if (window.jQuery) {
    // jQuery is available.

    // Print the jQuery version, e.g. "1.0.0":
    console.log(window.jQuery.fn.jquery);
}

This method is used by http://html5boilerplate.com and others.

Markus Amalthea Magnuson
  • 8,415
  • 4
  • 41
  • 49
6

My preference is:

console.debug("jQuery "+ (jQuery ? $().jquery : "NOT") +" loaded")

Result:

jQuery 1.8.0 loaded

sshel207
  • 101
  • 1
  • 7
  • I get : undefined – Joe L. Jul 23 '18 at 20:05
  • This is a bit better, but logging isn't that useful when working on an app. You're better off saving a variable first. ```let has$ = typeof jQuery === 'function'; console.log('jQuery' + (has$ ? ` version ${jQuery.fn.jquery} loaded.` : ' is not loaded.')); ``` –  Aug 02 '18 at 22:38
4

In one line and the minimum of keystrokes (oops!):

alert($().jquery);
David White
  • 621
  • 1
  • 10
  • 23
3

You should actually wrap this in a try/catch block for IE:

// Ensure jquery is loaded -- syntaxed for IE compatibility
try
{
    var jqueryIsLoaded=jQuery;
    jQueryIsLoaded=true;
}
catch(err)
{
    var jQueryIsLoaded=false;
}
if(jQueryIsLoaded)
{
    $(function(){
        /** site level jquery code here **/
    });
}
else
{
    // Jquery not loaded
}
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • IE doesn't handle `typeof jQuery != 'undefined'`? Is this true for every IE or just the older ones? – JJJ Aug 07 '11 at 16:26
  • Pretty sure it's for the legacy versions. Still what's more embarrassing than a big ol' JS error preventing your page from working, regardless if it's the browser's fault ;) – AlienWebguy Aug 07 '11 at 16:45
  • This has really helped me a lot. I was struggling to get the exact behavior across Firefox and IE for checking whether jQuery is loaded or not using WatiN. Now with this workaround, I am very happy. Thanks to you! – rahoolm Feb 03 '14 at 08:23
2

Go to the developer's Tool > console and write one of the following command jQuery.fn.jquery console.log(jQuery().jquery);

2

With optional chaining operator (?.) this can be done with window.jQuery?.fn?.jquery. This one liner also works if jQuery isn't loaded at all.

Krzysztof Przygoda
  • 1,217
  • 1
  • 17
  • 24
1

As per Template monster blog, typing, these below scripts will give you the version of the jquery in the site you are traversing now.

 1. console.log(jQuery.fn.jquery);
 2. console.log(jQuery().jquery);
aniruddha
  • 689
  • 8
  • 29
-1

Maybe self explanatory, but simplest method I have found is looking at the version commented in the actual file (typically jquery.min.js). Alternatively,if something like code.jquery.com is used, version number is in https reference (ie https://code.jquery.com/jquery-1.11.3.js).

enter image description here

Mark
  • 1
  • 2
-2
if (jQuery){
   //jquery loaded
}

.....

genesis
  • 50,477
  • 20
  • 96
  • 125
  • 1
    Some other libraries use `$`. – Jared Farrish Aug 07 '11 at 16:18
  • 2
    What if they're using Prototype? Or anything else that uses `$`? It's not unique to jQuery. – JJJ Aug 07 '11 at 16:18
  • What if `jQuery` isn't loaded? (It'll throw a `ReferenceError`). The above answer is correct, and that's why people are down-voting your answer. –  Aug 02 '18 at 22:33