How to check if a JavaScript function is defined

I encountered a small issue on javascript while doing a small hack for a VBulletin plugin. Sometimes a javascript function was included, sometimes not.
So I needed a way to check if that function was or wasn’t defined so I can call it safely (without any error message). In case you ever need something like this .. here is the code :)

JAVASCRIPT [Show Plain Code]:
  1. if(typeof functionNameToCheck == ‘function’) {
  2.         //you can now safely call your function
  3. }

One Comment

  1. Junaid Atari says:

    Cool, Your rock. :) Nice trick.

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes