tags:

views:

141

answers:

3

How can I verify if a function was defined already?

Will this work?

if(window.opener.MyFunctionBlah) {  ...  }
+2  A: 

Yes. Functions are just properties of objects like any other, so you can treat them as such. The conditional you posted above will return true if that function (or any member of window.opener called MyFunctionBlah) is defined and non-null.

Marc W
Technically, non-null, non-false, non-zero, and non-empty-string... but hopefully, any name that looks like a function would actually be a function if it exists.
Matthew Crumley
+6  A: 
if (typeof yourFunctionName == 'function') {
    yourFunctionName();
}
marduk
If i remember correctly, (typeof function(){}) is 'object' in IE.
I.devries
Nope, it's "function" as well. I think you're thinking of typeof for arrays.
ajm
Sorin Mocanu
+2  A: 

if( window.opener.MyFunctionBlah && ... is not necessary because typeof will return 'undefined' if the given property is not defined.

A simple if( typeof window.opener.myFunctionBlah == 'function' is enough.

romac