« News from Thursday, February 15, 2007 | Main | News from Friday, February 16, 2007 »

Check if a Javascript Function Exists or Is Defined

To check if a Javascript function exists before calling it, try this:

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

I was surprised how hard it was to google the right answer to this...but I found it at CodingForums.

Technorati Tags: , , ,

Comments

andho

how about if the function name is in a variable like this:
var funcName = 'myFunc';

then i want to check if the function stored in funcName exists:
if (typeof eval(funcName) == 'function') {

this gives an error in:
myFunc is not defined

in firebug plugin for firefox

steve

Of course eval() cannot evaluate this variable, because it might be undefined. :)
But it works in this way:

if (eval("typeof " + funcName + " == 'function'")) {
}

Søren Lund

I solved the 'function name is in a variable'-problem with the code below:

if (typeof funcName == 'string' &&
eval('typeof ' + funcName) == 'function') {

eval(funcName+'()');


You have to eval the typeof and not typeof the eval.

Ketan Solanki

Soren,

if typeof funcName == 'string'
then eval('typeof ' + funcName) == 'function')
will the same as eval('string')
and you will get the error saying 'string' is undefined

jon

exactly what i needed, works beautifully.

tiny caveat when trying to test the existence of a function off of a jquery selector. have to select first...

if(typeof $("#datePicker").datepicker == 'function')

db0

Is there a way to check the same thing through php? For example I want to check if the scrptalicious blind function exists before hiding an html element.

Ehsun

Thanks! very fruitful post

ken neville

How often are you calling functions that don't exist? Sounds a little overdefensive to me :)

Instead, try:

myFunction && myFunction.call && myFunction() || otherFunction();

This eliminates an unneeded "if()" closure.

Thanks!

Short and to the point, and it worked perfectly! Much appreciated!

Joe

Why not just use...
if(document['funcName']) // call function

Lachlan

First piece of code I tried, and it worked perfectly. Many thanks.

Tavish

Ken,

That will still call otherFunction() if myFunction() doesn't return truthy.

However, I like the elegance. Is there a way to check for existence only in this way; regardless of the return value for the function?

Big Dirty

Thanks man, if I was a chick I'd send a pic of my titties for thanks!

GreySkull

Hey Dude !!!

Thanx a lot for this ... was lookin all over the internet for this solution !!!

Phillip

Looks like a pretty simplistic solution. Glad I found this post, as I've been having a rough time trying to figure out how to do this recently. Many thanks!

Deanit

Thanks. Very good solution!

rajesh sharma

If you just need to check if a function exists: you can just use if(window.function_name) [if the function is not contained in a class] OR if(className.function_name) [if the function is supposed to be in a class],
Remember not to include the "()" while checking :)

Matt Densley

IE seems to return "object" sometimes, even for functions.

Adr

How about using try{..}catch(e){..} it may be more suitable in many situations.

TeMc

@jon:

Not true, a special jQuery function is pretty much always introduced using $.fn

so instead of $("#bla").myFunction() use $.fn.myFunction() to test if it's a function

Post a comment

Comments are moderated, and will not appear on this weblog until the author has approved them.

If you have a TypeKey or TypePad account, please Sign In.