How to check jQuery plugin and functions exists?

淺唱寂寞╮ 提交于 2019-11-28 05:14:16
if ($.fn.marquee) {
    // there is some jquery plugin named 'marquee' on your page
}

You can also do this. Let me take jQuery marquee example.

This is good if you are using only jQuery.

if($().marquee) {
    // marquee is loaded and available
}

OR

if($.fn.marquee !== undefined) {
    // marquee is loaded and available
}

Similar to above but Safe when you are using other JS frameworks Mootools etc.

if(jQuery().marquee) {
    // marquee is loaded and available
}

OR

if(jQuery.fn.marquee !== undefined) {
    // marquee is loaded and available
}

Slightly better:

if ($.isFunction($.fn.marquee)) {
    // ...
}

Maybe a little overkill, but this will ensure that it's at least a function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!