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
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.
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);
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.
If you want to use jQuery. UI you have to include jQuery.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With