如何检查加载了什么版本的jQuery ?

时间:2021-09-27 07:02:56

How do I check which version of jQuery is loaded on the client machine? The client may have jQuery loaded but I don't know how to check it. If they have it loaded how do I check the version and the prefix such as:

如何检查客户端机器上装载了哪个jQuery版本?客户端可能已经加载了jQuery,但我不知道如何检查它。如果他们已经加载了,我如何检查版本和前缀,例如:

$('.class')
JQuery('.class')

8 个解决方案

#1


665  

if (typeof jQuery != 'undefined') {  
    // jQuery is loaded => print the version
    alert(jQuery.fn.jquery);
}

#2


25  

You can just check if the jQuery object exists:

您可以检查jQuery对象是否存在:

if( typeof jQuery !== 'undefined' ) ... // jQuery loaded

jQuery().jquery has the version number.

jQuery()。jquery有版本号。

As for the prefix, jQuery should always work. If you want to use $ you can wrap your code to a function and pass jQuery to it as the parameter:

至于前缀,jQuery应该始终有效。如果您想使用$,您可以将您的代码包装成一个函数,并将jQuery作为参数传递给它:

(function( $ ) {
    $( '.class' ).doSomething();  // works always
})( jQuery )

#3


15  

…just because this method hasn't been mentioned so far - open the console and type:

只是因为这个方法还没有被提到——打开控制台和类型:

$ === jQuery

As @Juhana mentioned above $().jquery will return the version number.

正如@Juhana上面提到的$()。jquery将返回版本号。

#4


10  

I have found this to be the shortest and simplest way to check if jQuery is loaded:

我发现这是检查jQuery是否加载的最短最简单的方法:

if (window.jQuery) {
    // jQuery is available.

    // Print the jQuery version, e.g. "1.0.0":
    console.log(window.jQuery.fn.jquery);
}

This method is used by http://html5boilerplate.com and others.

这个方法由http://html5boilerplate.com等人使用。

#5


6  

$.fn.jquery

If you get back a version number -- usually as a string -- then jQuery is loaded and that is what version you're working with. If not loaded then you should get back undefined or maybe even an error.

如果返回一个版本号(通常是字符串),那么jQuery就会被加载,这就是您使用的版本。如果没有加载,那么应该返回未定义的,甚至可能是错误。

Pretty old question and I've seen a few people that have already mentioned my answer in comments. However, I find that sometimes great answers that are left as comments can go unnoticed; especially when there are a lot of comments to an answer you may find yourself digging through piles of them looking for a gem. Hopefully this helps someone out!

很老的问题,我见过一些人在评论中提到了我的答案。然而,我发现有时作为评论而留下的伟大答案会被忽视;特别是当你对一个答案有很多评论的时候,你可能会发现自己在翻找一大堆的答案来寻找一个宝石。希望这能帮助别人!

#6


4  

My preference is:

我的偏好是:

console.debug("jQuery "+ (jQuery ? $().jquery : "NOT") +" loaded")

Result:

结果:

jQuery 1.8.0 loaded

jQuery 1.8.0加载

#7


2  

You should actually wrap this in a try/catch block for IE:

实际上,你应该把这个包在try/catch块中,以便IE:

// Ensure jquery is loaded -- syntaxed for IE compatibility
try
{
    var jqueryIsLoaded=jQuery;
    jQueryIsLoaded=true;
}
catch(err)
{
    var jQueryIsLoaded=false;
}
if(jQueryIsLoaded)
{
    $(function(){
        /** site level jquery code here **/
    });
}
else
{
    // Jquery not loaded
}

#8


-2  

if (jQuery){
   //jquery loaded
}

.....

.....

#1


665  

if (typeof jQuery != 'undefined') {  
    // jQuery is loaded => print the version
    alert(jQuery.fn.jquery);
}

#2


25  

You can just check if the jQuery object exists:

您可以检查jQuery对象是否存在:

if( typeof jQuery !== 'undefined' ) ... // jQuery loaded

jQuery().jquery has the version number.

jQuery()。jquery有版本号。

As for the prefix, jQuery should always work. If you want to use $ you can wrap your code to a function and pass jQuery to it as the parameter:

至于前缀,jQuery应该始终有效。如果您想使用$,您可以将您的代码包装成一个函数,并将jQuery作为参数传递给它:

(function( $ ) {
    $( '.class' ).doSomething();  // works always
})( jQuery )

#3


15  

…just because this method hasn't been mentioned so far - open the console and type:

只是因为这个方法还没有被提到——打开控制台和类型:

$ === jQuery

As @Juhana mentioned above $().jquery will return the version number.

正如@Juhana上面提到的$()。jquery将返回版本号。

#4


10  

I have found this to be the shortest and simplest way to check if jQuery is loaded:

我发现这是检查jQuery是否加载的最短最简单的方法:

if (window.jQuery) {
    // jQuery is available.

    // Print the jQuery version, e.g. "1.0.0":
    console.log(window.jQuery.fn.jquery);
}

This method is used by http://html5boilerplate.com and others.

这个方法由http://html5boilerplate.com等人使用。

#5


6  

$.fn.jquery

If you get back a version number -- usually as a string -- then jQuery is loaded and that is what version you're working with. If not loaded then you should get back undefined or maybe even an error.

如果返回一个版本号(通常是字符串),那么jQuery就会被加载,这就是您使用的版本。如果没有加载,那么应该返回未定义的,甚至可能是错误。

Pretty old question and I've seen a few people that have already mentioned my answer in comments. However, I find that sometimes great answers that are left as comments can go unnoticed; especially when there are a lot of comments to an answer you may find yourself digging through piles of them looking for a gem. Hopefully this helps someone out!

很老的问题,我见过一些人在评论中提到了我的答案。然而,我发现有时作为评论而留下的伟大答案会被忽视;特别是当你对一个答案有很多评论的时候,你可能会发现自己在翻找一大堆的答案来寻找一个宝石。希望这能帮助别人!

#6


4  

My preference is:

我的偏好是:

console.debug("jQuery "+ (jQuery ? $().jquery : "NOT") +" loaded")

Result:

结果:

jQuery 1.8.0 loaded

jQuery 1.8.0加载

#7


2  

You should actually wrap this in a try/catch block for IE:

实际上,你应该把这个包在try/catch块中,以便IE:

// Ensure jquery is loaded -- syntaxed for IE compatibility
try
{
    var jqueryIsLoaded=jQuery;
    jQueryIsLoaded=true;
}
catch(err)
{
    var jQueryIsLoaded=false;
}
if(jQueryIsLoaded)
{
    $(function(){
        /** site level jquery code here **/
    });
}
else
{
    // Jquery not loaded
}

#8


-2  

if (jQuery){
   //jquery loaded
}

.....

.....