Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 767
multimediaxp Avatar asked Mar 29 '12 22:03

multimediaxp


People also ask

How do you check if you have jQuery installed?

You can test if jQuery is loaded by opening your javascript console (in Chrome: Settings > More tools > Javascript console). Where the little blue arrow appears type: if(jQuery) alert('jQuery is loaded'); Press enter.

How do I know what version of jQuery I have?

Type this command in the Chrome Developer Tools Javascript console window to see what version of the jQuery is being used on this page: console. log(jQuery(). jquery);

Is jQuery different from jQuery UI?

jQuery is the core library. jQueryUI is built on top of it. If you use jQueryUI, you must also include jQuery. jQuery Tabs preceded jQueryUI library.

Is jQuery UI include in jQuery?

If you want to use jQuery. UI you have to include jQuery.


1 Answers

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.

like image 106
Cᴏʀʏ Avatar answered Sep 19 '22 19:09

Cᴏʀʏ