1

How can I verify if a function was defined already?

Will this work?

if(window.opener.MyFunctionBlah) {  ...  }

2 Answers 2

6
if (typeof yourFunctionName === 'function') {
    yourFunctionName();
}
Sign up to request clarification or add additional context in comments.

4 Comments

If i remember correctly, (typeof function(){}) is 'object' in IE.
Nope, it's "function" as well. I think you're thinking of typeof for arrays.
In this specific case it may be "if (window.opener && typeof window.opener.MyFunctionBlah == 'function')". You can't expect window.opener to be always an object...
In the case of window.opener even if MyFunctionBlar is a function. In IE typeof window.opener.MyFunctionBlar will be object.
2

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.