[thelist] check for existence of window by name?

Cymbala, Greg Greg.Cymbala at Den.Galileo.com
Mon Mar 25 14:00:01 CST 2002

Well, that confused me at first, too.  newWindow (on the 1st line of the
script) is a JavaScript variable that is a reference to the window object
that is created with the window.open() call.  I almost expected to be able
to reference the newly created window by the name parameter of the
window.open() method, but that didn't work.  That name doesn't seem to
matter in this context.

Here's how it works:

We first declare the newWindow variable.  Since we don't at first assign any
value or object to that variable, it's undefined.  Testing an undefined
variable in an if statement returns false.  So, if the first if statement in
the testWindow function returns false, we know that we haven't called our
openNewWindow function.

But, if the newWindow variable is not null or undefined, then we know it's
probably a reference to our window object and we can test its closed

There's no significance to the "my" and "new" prefixes.


-----Original Message-----
From: AtdtXav [mailto:atdtxav at yahoo.com]
Sent: Monday, March 25, 2002 12:22 PM
To: thelist at lists.evolt.org
Subject: RE: [thelist] check for existence of window by name?

This is incredibly useful, greg, and I think I might be able to make it from
here, but could you explain to me a little bit about how this works?

I see myNewWindow is the window name, and yet you're checking for the
existence of newWindow.  Why is this?  Is this a generic search for a new
window, or am I unaware of "my" and "new" prefixes within javascript?

Thanks for your email

--- "Cymbala, Greg" <Greg.Cymbala at Den.Galileo.com> wrote:
> Try this in a page:
> <HTML>
> <HEAD>
> <SCRIPT LANGUAGE="JavaScript">
> var newWindow;
> // Try uncommenting/commenting the following line: //openNewWindow();
> function testWindow()
> {
> 	if (newWindow)
> 	{
> 		if (newWindow.closed)
> 		{
> 			alert("Not open.  Opening it.");
> 			openNewWindow();
> 		}
> 		else
> 		{
> 			alert("It's open!");
> 		}
> 	}
> 	else
> 	{
> 		alert("Window was never opened.  Opening it.");
> 		openNewWindow();
> 	}
> }
> function openNewWindow()
> {
> 	newWindow = window.open("http://www.google.com",
> "myNewWindow");
> }
> </HEAD>
> <BODY>
> <P><input type="button" onclick="testWindow()" value="Test" /></P>
> </BODY>
> </HTML>

The information in this electronic mail message is sender's business
Confidential and may be legally privileged.  It is intended solely for the
addressee(s).  Access to this Internet electronic mail message by anyone
else is unauthorized.  If you are not the intended recipient, any
disclosure, copying, distribution or any action taken or omitted to be taken
in reliance on it is prohibited and may be unlawful.
The sender believes that this E-mail and any attachments were free of any
virus, worm, Trojan horse, and/or malicious code when sent. This message and
its attachments could have been infected during  transmission. By reading
the message and opening any attachments, the recipient accepts full
responsibility for taking protective and remedial action about viruses and
other defects. Galileo International is not liable for any loss or damage
arising in any way from this message or its attachments.

More information about the thelist mailing list