06.10.2008: JavaScript: Find out if function exists

I've been working on a site concept which involves several pure HTML- and Javascript-based pages, to be uses as mere demonstrational material. While adding several niceties like modal windows based on my fork of Thickbox, ie. Thickbox Redux, I've stumbled over some strange errors Firebug throw out during the document load process.

Thus I had to implement some kind of "if function exists"-magic to avoid getting errors on the debugging console (not to mention that this properly might have side-effects like totally disabling my JS efforts at all and similar worst-case scenarios) - which is quite hard to find out, I have to say.

Simple, but effective solution is as follows:

if(typeof yourFunctionName == 'function') { yourFunctionName(); }

Found via Idealog: Check if a Javascript Function Exists or Is Defined and (Paul Bennett) Javascript: checking if a function exists.

0 Responses to JavaScript: Find out if function exists

  1. There are currently no comments.