最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I detect if Jquery and Jquery UI are installed, and what versions are installed? - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Apr 14, 2013 at 6:57 abatishchev 100k88 gold badges301 silver badges442 bronze badges asked Mar 29, 2012 at 22:22 Eduardo Ponce de LeonEduardo Ponce de Leon 9,69613 gold badges55 silver badges86 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 17

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论