[RESOLVED]how to check js function exist or not

how to check js function exist or not at client side before calling it?

You can use the below code to check if function exists or not

 //Function to check if javascript function existis or not
  function checkfunctionexists()
  {
    if (typeof testfunction == 'function') { 
      //if Function exists then call your function
        testfunction(); 
    }else{
        alert('Function dont exists!');
    }
  }

Complete Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
<script>
  //Your sample functin here
  function testfunction()
  {
      alert("sample function");
  }
  
  //Function to check if javascript function existis or not
  function checkfunctionexists()
  {
    if (typeof testfunction == 'function') { 
      //if Function exists then call your function
        testfunction(); 
    }else{
        alert('Function dont exists!');
    }
  }
  </script>
</head>
<body>
<input type="button" value="check" onclick="checkfunctionexists()"/>
</body>
</html>

Source URL

Hi mou_inn,

Thanks for your post.

As for your problem, you could refer to the following similar issue.

Besides, jQuery.isFunction() could determine if the argument passed is a Javascript function object. For more information about it, you could check the below link.

Hope it will be helpful to you.

Best Regards,

Fei Han

Leave a Reply