JavaScript
Article

jQuery does function exist

By Sam Deering

Checking if a function exists using JavaScript you can do the following:

if(typeof $form.validate == 'function')
{
    console.log('function is here!');
}
else
{
    console.log('function is absent... naughty!');
}

Example: Check if a the md5 function exists:

if(typeof window.md5 == 'function') {
    // function exists, so we can now call it
    md5(286);
}

And to check if a jQuery function exists:

if( jQuery.isFunction(jQuery.fn.rtFunction) ){
  // function exists, so we can now call it
  jQuery(document).rtFunction();
}

No Reader comments

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in JavaScript, once a week, for free.