Blankman
Blankman

Reputation: 267330

How can I verify if a function was defined in JavaScript?

How can I verify if a function was defined already?

Will this work?

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

Upvotes: 1

Views: 362

Answers (2)

gehsekky
gehsekky

Reputation: 3219

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

Upvotes: 6

Marc W
Marc W

Reputation: 19251

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.

Upvotes: 2

Related Questions