如何检查是否已加载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 Validation插件使用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屋!