[thelist] check existence of a javascript function

Richard Bennett richard.bennett at skynet.be
Thu Jan 24 11:18:11 CST 2002


Hi,
there is a very nice way to test if a function has been declared, or a
variable has been initialized.
When used in the document itself, you can use the "self" object, which
refers to the window object.
There are two ways of testing , either by passing the variable as a string,
or a property, like this:

if(self.msg) alert(msg)
else alert("msg is not defined")

or:

if(self['msg']) alert(msg)
else alert("msg is not defined")

So both the above will show the alert "msg is not defined".
If however you define the msg variable :

var msg='hi'

or declare a function called msg :

function msg(){
}

Then it's value will be shown in the first alert.

So in the case of you popups, if you used something like this to set the
opener:
if (remote.opener == null) remote.opener = window;

then opener is the window object, like self, so this should work fine:

if(opener.msg) alert(msg)
else alert("msg is not defined")

or:

if(opener['msg']) alert(msg)
else alert("msg is not defined")

This should work just the same for functions or variables.

Hope it's right, and helps, and that .jeff doesn't object to the coding
style to much...

Cheers,
Richard.



----- Original Message -----
From: "Emily E Liang" <Emily.Liang at morganstanley.com>

> 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!!!






More information about the thelist mailing list