[thelist] check existence of a javascript function

Liorean Liorean at user.bip.net
Thu Jan 24 13:30:15 CST 2002


At 11:09 2002-01-24 -0500, Emily E Liang wrote:
>Hi,
>I'm trying to figure out how to check the existence of a javascript
>function in the opener window.  I tried all of the following and none
>seemed to work.  All returned a javascript error of "opener.getAppId()
>is not a function" error when the junction does not exist.  Any help is
>greatly appreciated!!!
>
>1) if (opener.getAppId() == undefined) {
>         //do something else
>     } else {
>         appId = opener.getAppId();
>     }
>
>
>2) if (!opener.getAppId) {
>         //do something else
>     } else {
>         appId = opener.getAppId();
>     }
>
>3) if (opener.getAppId() == null) {
>         //do something else
>     } else {
>         appId = opener.getAppId();
>     }

First of all, if you use "opener.getAppId()", you'll try to execute the
function. If it then doesn't exist, you'll get an error.

Try searching for the existence of the function by using:

>if(opener.getAppId?true:false){
>   ...
>}


or, to be fully correct with the upcoming JavaScript 2.0 (ECMA 262-4,
ECMAScript 4):

>if(typeof opener.getAppId!="undefined"){
>   ...
>}


Observe that null, false, 0, '' evaluates to false in the first case, but
that null is an object, false a boolean, 0 a number and '' a string, and
thus are determined to exist in the second case. Also, the "?true:false" in
the first case bit can be left out, but that's a bad habit.


Also, since opener might be closed, you better check for that first, too.

// Liorean




More information about the thelist mailing list