40

Possible Duplicate:
jQuery test for whether an object has a method?

I want to set if Function Exists before Calling javascript can you help me how do this and apply on this script

$(document).ready(function() {
   $(".cs-text-cut").lettering('words');
});
Community
  • 1
  • 1
Amjad Ali
  • 417
  • 1
  • 4
  • 5
  • @Alexei: Well, judging from the example, I think it's exactly the same. And even if not, the solution (using `$.isFunction`) is still the same and is easily transferable. – Felix Kling Dec 01 '12 at 07:31
  • @FelixKling, you are right, exact dup. Somehow interpreted it broade (as if one used element, not result of jQuery selector). – Alexei Levenkov Dec 01 '12 at 07:49

4 Answers4

105

I'm assuming that you're wanting to check and make sure that lettering exists, try this:

http://api.jquery.com/jQuery.isFunction/

Here's an example:

if ( $.isFunction($.fn.lettering) ) {
    $(".cs-text-cut").lettering('words');
}
gregwhitworth
  • 2,136
  • 4
  • 22
  • 33
  • 1
    +1. for using `isFunction`, note that the condition is wrong - should not be checking one on window. – Alexei Levenkov Dec 01 '12 at 07:10
  • 1
    The following works as expected: `if ( $.isFunction(window.lettering) ) {` If you have a shorthand way or better way feel free to let me know and I'll update the suggestion. – gregwhitworth Dec 01 '12 at 07:23
  • 3
    The function will actually be in `$.fn.lettering`, not `window.lettering`. – Michael Mior Dec 01 '12 at 07:29
  • 1
    it would be better if you use this way `if ( typeof $.fn.lettering!== 'undefined' && $.isFunction($.fn.lettering) ) { $(".cs-text-cut").lettering('words'); }` – Muhammad Tahir Apr 23 '15 at 13:06
  • 5
    As of jQuery 3.3, jQuery.isFunction() has been deprecated. In most cases, its use can be replaced by ```typeof x === "function"```. – bubencode Nov 27 '20 at 13:25
26

Use this to check if function exists.

<script>
if ( typeof function_name == 'function' ) { 
        //function_name is a function
}
else
{
 //do not exist
}
</script>
harikrishnan.n0077
  • 1,927
  • 4
  • 21
  • 27
8

If it's the lettering function you want to test for, you can do so like this;

$(document).ready(function() {
    var items = $(".cs-text-cut");
    if (items.lettering) {
        items.lettering('words');
    }
});

Or, if you want to make absolutely sure items.lettering is a function before attempting to call it, you can do this:

$(document).ready(function() {
    var items = $(".cs-text-cut");
    if (typeof items.lettering === "function") {
        items.lettering('words');
    }
});

Or, if you really don't control the environment so you don't really know if the lettering function call is going to work or not and might even throw an exception, you can just put an exception handler around it:

$(document).ready(function() {
    try {
        $(".cs-text-cut").lettering('words');
    } catch(e) {
        // handle an exception here if lettering doesn't exist or throws an exception
    }
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

typeof $({}).lettering == 'function' or $.isFunction($({}).lettering) should return a boolean for whether it's available yet or not.

inhan
  • 7,394
  • 2
  • 24
  • 35