如何检查是否加载了 jQuery 插件? [英] How can I check if a jQuery plugin is loaded?
问题描述
有没有办法检查某个插件是否可用?
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.
例如,我希望 jQuery 验证插件使用 dateJS 库来检查给定日期是否有效.如果 dateJS 可用,在 jQuery Valdation 插件中检测的最佳方法是什么?
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 插件是 jQuery 作用域上的命名空间.您可以运行一个简单的检查来查看命名空间是否存在:
Generally speaking, jQuery plugins are namespaces on the jQuery scope. You could run a simple check to see if the namespace exists:
if(jQuery().pluginName) {
//run plugin dependent code
}
dateJs 然而不是一个 jQuery 插件.它修改/扩展 javascript 日期对象,并且不作为 jQuery 命名空间添加.您可以检查您需要的方法是否存在,例如:
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:
if(Date.today) {
//Use the dateJS today() method
}
但您可能会遇到 API 与原生 Date API 重叠的问题.
But you might run into problems where the API overlaps the native Date API.
这篇关于如何检查是否加载了 jQuery 插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!