Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if statically named JavaScript function exists to prevent errors

Tags:

I have a script on my website that calls a statically named function when called:

childLoad(); 

The childLoad() function is not always defined though it is always called. How can I prevent the script from calling this function if it does not exist?

like image 388
macintosh264 Avatar asked Sep 26 '11 18:09

macintosh264


People also ask

How do you check JavaScript function exists or not?

Please, some explanation. In two cases, simply call function_exists with the name of function parameter to check existence, returns bool.

How do we know if a function exists?

To prevent the error, you can first check if a function exists in your current JavaScript environment by using a combination of an if statement and the typeof operator on the function you want to call.

How do you check function is defined or not in jQuery?

With jQuery. isFunction() you may test a parameter to check if it is (a) defined and (b) is of type "function." Since you asked for jQuery, this function will tickle your fancy.

How do you exit a function in JavaScript?

Sometimes when you're in the middle of a function, you want a quick way to exit. You can do it using the return keyword. Whenever JavaScript sees the return keyword, it immediately exits the function and any variable (or value) you pass after return will be returned back as a result.


1 Answers

if ( typeof childLoad == 'function' ) {      childLoad();  } 
like image 63
Michael Jasper Avatar answered Oct 21 '22 06:10

Michael Jasper