如何检查是否加载了 jQuery 插件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/400916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check if a jQuery plugin is loaded?
提问by Vitor Silva
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 插件中检测的最佳方法是什么?
回答by Eran Galperin
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 与本机 Date API 重叠的问题。
回答by rmirabelle
If we're talking about a proper jQuery plugin (one 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
回答by Suso Guez
To detect jQuery plugins I found more accurate to use the brackets:
为了检测 jQuery 插件,我发现使用括号更准确:
if(jQuery().pluginName) {
//run plugin dependent code
}
回答by trante
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)
回答by HasanG
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/
回答by Joshua Pekera
Run this in your browser console of choice.
在您选择的浏览器控制台中运行它。
if(jQuery().pluginName){console.log('bonjour');}
if(jQuery().pluginName){console.log('bonjour');}
If the plugin exists it will print out "bonjour" as a response in your console.
如果插件存在,它将在您的控制台中打印出“bonjour”作为响应。
回答by Soviut
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 的回答保持谨慎并检查插件是否存在也没有什么坏处。
回答by ceejayoz
This sort of approach should work.
这种方法应该有效。
var plugin_exists = true;
try {
// some code that requires that plugin here
} catch(err) {
plugin_exists = false;
}