Eduardo Ponce de Leon
Eduardo Ponce de Leon

Reputation: 9696

How can I detect if Jquery and Jquery UI are installed, and what versions are installed?

I am creating a script in JS that will be called from external sites, but my code requires Jquery to work, specially 1.7 and 1.8 for UI, I found a way to check if jquery is installed and get the version:

$().jquery

But this will give me back a string with dots (1.6.1); is there already a function to check if the version installed is older than the one that I required?

I also need the same for the UI library, i found this but I am not very sure if it works properly, or maybe I don't know how o use it:

//Get version:
$.ui.version
//Comnpare version
var version_required = 1.7.1
version = $.ui ? $.ui.version || "pre "+version_required : 'not found';

Thanks

Upvotes: 11

Views: 8210

Answers (1)

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107498

This might work for you:

if (typeof jQuery != 'undefined' && /[1-9]\.[7-9].[1-9]/.test($.fn.jquery)) {
    // jQuery is loaded and is at least version 1.7.1
}

Likewise, it's almost the same for the UI:

if (typeof jQuery.ui != 'undefined' && /[1-9]\.[7-9].[1-9]/.test($.ui.version)) {
    // jQuery UI is loaded and is at least version 1.7.1
}

First it checks to make sure jQuery is available and then it uses some simple regex pattern to test that the version numbers are within an acceptable range.

UPDATE: This will also work with jQuery 2 and 3.

Upvotes: 17

Related Questions