如何检测是否安装了Jquery和Jquery UI,以及安装了哪些版本? [英] How can I detect if Jquery and Jquery UI are installed, and what versions are installed?

查看:102
本文介绍了如何检测是否安装了Jquery和Jquery UI,以及安装了哪些版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JS中创建一个将从外部站点调用的脚本,但是我的代码需要Jquery才能工作,特别是1.7和1.8用于UI,我找到了一种方法来检查是否安装了jquery并获得版本:

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

但这会给我一个带点的字符串(1.6.1);是否已经有一个功能来检查安装的版本是否比我要求的版本旧?

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?

我还需要相同的UI库,我发现这个但是我我不太确定它是否正常工作,或者我不知道如何使用它:

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';

谢谢

推荐答案

这可能适合你:

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

同样,UI的几乎相同:

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
}

首先检查以确保jQuery可用,然后使用一些简单的正则表达式模式来测试版本号是否在可接受的范围内。

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 :这也适用于jQuery 2和3.

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

这篇关于如何检测是否安装了Jquery和Jquery UI,以及安装了哪些版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆