如何检查jQuery插件是否已加载?

时间:2023-01-24 17:59:00

Is there any way to check if a particular plugin is available?

有没有办法检查某个插件是否可用?

Imagine that you are developing a plugin that depends on another plugin being loaded.

假设您正在开发一个依赖于加载另一个插件的插件。

For example I want the jQuery Validation plugin to use the dateJS library to check if a given date is valid. What would be the best way to detect, in the jQuery Valdation plugin if the dateJS was available?

例如,我希望jQuery验证插件使用dateJS库检查给定的日期是否有效。如果dateJS可用,在jQuery Valdation插件中最好的检测方法是什么?

8 个解决方案

#1


324  

Generally speaking, jQuery plugins are namespaces on the jQuery scope. You could run a simple check to see if the namespace exists:

一般来说,jQuery插件是jQuery范围的名称空间。您可以运行一个简单的检查,看看名称空间是否存在:

 if(jQuery().pluginName) {
     //run plugin dependent code
 }

dateJs however is not a jQuery plugin. It modifies/extends the javascript date object, and is not added as a jQuery namespace. You could check if the method you need exists, for example:

然而,dateJs并不是jQuery插件。它修改/扩展javascript日期对象,不作为jQuery名称空间添加。你可以检查你需要的方法是否存在,例如:

 if(Date.today) {
      //Use the dateJS today() method
 }

But you might run into problems where the API overlaps the native Date API.

但是,您可能会遇到API与本机日期API重叠的问题。

#2


86  

If we're talking about a proper jQuery plugin (on that extends the fn namespace), then the proper way to detect the plugin would be:

如果我们讨论的是一个合适的jQuery插件(扩展了fn名称空间),那么检测这个插件的正确方法应该是:

if(typeof $.fn.pluginname !== 'undefined') { ... }

Or because every plugin is pretty much guaranteed to have some value that equates to true, you can use the shorter

或者因为每个插件都保证有一些值等于true,你可以使用更短的

if ($.fn.pluginname) { ... }

BTW, the $ and jQuery are interchangable, as the odd-looking wrapper around a plugin demonstrates:

顺便说一句,$和jQuery是可以互换的,就像插件周围奇怪的包装一样:

(function($) {
    //
})(jQuery))

the closure

关闭

(function($) {
    //
})

is followed immediately by a call to that closure 'passing' jQuery as the parameter

紧接着是调用该闭包“传递”jQuery作为参数?

(jQuery)

the $ in the closure is set equal to jQuery

闭包中的$设置等于jQuery。

#3


11  

To detect jQuery plugins I found more accurate to use the brackets:

为了检测jQuery插件,我发现使用括号更准确:

if(jQuery().pluginName) {
    //run plugin dependent code
}

#4


8  

for the plugins that doesn't use fn namespace (for example pnotify), this works:

对于不使用fn命名空间(例如pnotify)的插件,这是有效的:

if($.pluginname) {
    alert("plugin loaded");
} else {
    alert("plugin not loaded");
}

This doesn't work:

这并不工作:

if($.fn.pluginname)

#5


2  

Run this in your browser console of choice.

在您选择的浏览器控制台运行此命令。

if(jQuery().pluginName){console.log('bonjour');}

如果(jQuery().pluginName){ console.log(“bonjour”);}

If the plugin exists it will print out "bonjour" as a response in your console.

如果插件存在,它会在你的控制台打印出“bonjour”作为响应。

#6


1  

This sort of approach should work.

这种方法应该有效。

var plugin_exists = true;

try {
  // some code that requires that plugin here
} catch(err) {
  plugin_exists = false;
}

#7


1  

I would strongly recommend that you bundle the DateJS library with your plugin and document the fact that you've done it. Nothing is more frustrating than having to hunt down dependencies.

我强烈建议您将DateJS库与您的插件捆绑在一起,并记录您已经完成的事实。没有什么比不得不寻找依赖性更令人沮丧的了。

That said, for legal reasons, you may not always be able to bundle everything. It also never hurts to be cautious and check for the existence of the plugin using Eran Galperin's answer.

也就是说,由于法律上的原因,你可能并不总是能够把所有的东西都打包。使用Eran Galperin的答案检查插件是否存在也没有坏处。

#8


1  

jQuery has a method to check if something is a function

jQuery有一个方法来检查某个函数是否为函数

if ($.isFunction($.fn.dateJS)) {
    //your code using the plugin
}

API reference: https://api.jquery.com/jQuery.isFunction/

API参考:https://api.jquery.com/jQuery.isFunction/

#1


324  

Generally speaking, jQuery plugins are namespaces on the jQuery scope. You could run a simple check to see if the namespace exists:

一般来说,jQuery插件是jQuery范围的名称空间。您可以运行一个简单的检查,看看名称空间是否存在:

 if(jQuery().pluginName) {
     //run plugin dependent code
 }

dateJs however is not a jQuery plugin. It modifies/extends the javascript date object, and is not added as a jQuery namespace. You could check if the method you need exists, for example:

然而,dateJs并不是jQuery插件。它修改/扩展javascript日期对象,不作为jQuery名称空间添加。你可以检查你需要的方法是否存在,例如:

 if(Date.today) {
      //Use the dateJS today() method
 }

But you might run into problems where the API overlaps the native Date API.

但是,您可能会遇到API与本机日期API重叠的问题。

#2


86  

If we're talking about a proper jQuery plugin (on that extends the fn namespace), then the proper way to detect the plugin would be:

如果我们讨论的是一个合适的jQuery插件(扩展了fn名称空间),那么检测这个插件的正确方法应该是:

if(typeof $.fn.pluginname !== 'undefined') { ... }

Or because every plugin is pretty much guaranteed to have some value that equates to true, you can use the shorter

或者因为每个插件都保证有一些值等于true,你可以使用更短的

if ($.fn.pluginname) { ... }

BTW, the $ and jQuery are interchangable, as the odd-looking wrapper around a plugin demonstrates:

顺便说一句,$和jQuery是可以互换的,就像插件周围奇怪的包装一样:

(function($) {
    //
})(jQuery))

the closure

关闭

(function($) {
    //
})

is followed immediately by a call to that closure 'passing' jQuery as the parameter

紧接着是调用该闭包“传递”jQuery作为参数?

(jQuery)

the $ in the closure is set equal to jQuery

闭包中的$设置等于jQuery。

#3


11  

To detect jQuery plugins I found more accurate to use the brackets:

为了检测jQuery插件,我发现使用括号更准确:

if(jQuery().pluginName) {
    //run plugin dependent code
}

#4


8  

for the plugins that doesn't use fn namespace (for example pnotify), this works:

对于不使用fn命名空间(例如pnotify)的插件,这是有效的:

if($.pluginname) {
    alert("plugin loaded");
} else {
    alert("plugin not loaded");
}

This doesn't work:

这并不工作:

if($.fn.pluginname)

#5


2  

Run this in your browser console of choice.

在您选择的浏览器控制台运行此命令。

if(jQuery().pluginName){console.log('bonjour');}

如果(jQuery().pluginName){ console.log(“bonjour”);}

If the plugin exists it will print out "bonjour" as a response in your console.

如果插件存在,它会在你的控制台打印出“bonjour”作为响应。

#6


1  

This sort of approach should work.

这种方法应该有效。

var plugin_exists = true;

try {
  // some code that requires that plugin here
} catch(err) {
  plugin_exists = false;
}

#7


1  

I would strongly recommend that you bundle the DateJS library with your plugin and document the fact that you've done it. Nothing is more frustrating than having to hunt down dependencies.

我强烈建议您将DateJS库与您的插件捆绑在一起,并记录您已经完成的事实。没有什么比不得不寻找依赖性更令人沮丧的了。

That said, for legal reasons, you may not always be able to bundle everything. It also never hurts to be cautious and check for the existence of the plugin using Eran Galperin's answer.

也就是说,由于法律上的原因,你可能并不总是能够把所有的东西都打包。使用Eran Galperin的答案检查插件是否存在也没有坏处。

#8


1  

jQuery has a method to check if something is a function

jQuery有一个方法来检查某个函数是否为函数

if ($.isFunction($.fn.dateJS)) {
    //your code using the plugin
}

API reference: https://api.jquery.com/jQuery.isFunction/

API参考:https://api.jquery.com/jQuery.isFunction/