Monday, July 22, 2013

Check if Function Exists Before Calling

When using scripts that are shared between different areas of a site, there may be cases where a function is called that doesn't exist. 
you can just check if the function exists before calling it to avoid the error:

Java Script :
if (typeof yourFunctionName == 'function') { yourFunctionName(); }else{ alert('Check yourFunctionName!'); }

Jquery:
Also could be:
$.isFunction(yourFunctionName)&&yourFunctionName()

or if you are sure that if its exists than its a function:
yourFunctionName&&yourFunctionName()

No comments:

Post a Comment