Javascript for detecting undefined windows -- what is wrong with this script?

H

Hal

Can someone please tell me what is wrong with this script?

<script language="JavaScript">
<!--
function HF_CloseAllChildren() {
if (g_win1) != undefined {
if ((g_win1) && !(g_win1.closed))
g_win1.close();
}
}
//-->
</script>

I am trying to close child windows (7 of them) from the parent window.
I only want to close windows that are undefined.

Thanks!
 
L

Lasse Reichstein Nielsen

Can someone please tell me what is wrong with this script?
<script language="JavaScript">

Should be:

That line is not needed.
function HF_CloseAllChildren() {
if (g_win1) != undefined {

The global "g_win1" variable doesn't appear to have been defined. What
is it supposed to contain? (From your later explanation, I assume it
contains a reference to a window object as returned by window.open)

The syntax is wrong. The entire condition of the "if" statement
must be inside parentheses.

When comparing with a basic value, you should use "!==" (and "==="),
instead of "!=", since the latter performs type conversion before
comparing. That is:

if (g_win1 !== undefined) {
if ((g_win1) && !(g_win1.closed))

Here you test whether the value of "g_win1" converts to the boolean
"true". If the value is undefined, then this test will also be false,
so the first test is not necessary.
I am trying to close child windows (7 of them) from the parent window.
I only want to close windows that are undefined.

Do you mean "windows that are NOT undefined"? Because "windows" that are
equal to "undefined" are not windows.


/L
 
H

Hal

Thanks for the reply.
The variable is assigned (and the window opened) in this script, which
is only executed when the user clicks a link on the parent page. It
is possible the user never clicks the link.

<script type="text/javascript">
function MM_openBrWindow(theURL,winName,features)
{
g_win1 = window.open(theURL,winName,features);
}
</script>


The body tag of the parent page looks like this:

<body OnUnload = "javascript:HF_CloseAllChildren();">

Which executes this script:

<script type="text/javascript">
function HF_CloseAllChildren() {
if ((g_win1) && !(g_win1.closed))
g_win1.close();
}
</script>

The problem is that when the user has not opened the child window,
she/he gets an error message saying that g_win1 is undefined. I
thought the line:

if ((g_win1) && !(g_win1.closed))

would evaluate to false if g_win is undefined and would not throw an
error. Is there any way I can accomplish this? I know that I can use
code to open the window just to close it, but that seems very clumsy
to me, especially since there are up to 7 child windows that can be
opened from this parent page. Any ideas?

Hal
 
D

DU

Hal said:
Thanks for the reply.
The variable is assigned (and the window opened) in this script, which
is only executed when the user clicks a link on the parent page. It
is possible the user never clicks the link.

<script type="text/javascript">

You should define the window object reference explicitly as a global
variable right here like this:

var g_win1;
function MM_openBrWindow(theURL,winName,features)
{
g_win1 = window.open(theURL,winName,features);
}
</script>


The body tag of the parent page looks like this:

<body OnUnload = "javascript:HF_CloseAllChildren();">

The "javascript:" part is unneeded and irrelevant. As long as you define
the default script language in the <head> part, then you never need to
add "javascript:" anywhere else.
Defining default scripting language in HTML 4.01:
http://www.w3.org/TR/html401/interact/scripts.html#default-script

<head>
....
<meta http-equiv="Content-Script-Type" content="text/javascript">
....
Which executes this script:

<script type="text/javascript">
function HF_CloseAllChildren() {
if ((g_win1) && !(g_win1.closed))
g_win1.close();
}
</script>

<script type="text/javascript">
function HF_CloseAllChildren()
{
if (g_win1 != null && !g_win1.closed)
{ if(g_win1.close) {g_win1.close();};
}

Note that recent Mozilla-based browsers will not honor g_win1.close() if
a particular user pref prevents it from working.

user_pref("dom.allow_scripts_to_close_windows", false);

You may also get a similar behavior in Opera 7.x. Note that you are
trying to close automatically a window without the user's explicit will.
The problem is that when the user has not opened the child window,
she/he gets an error message saying that g_win1 is undefined.

And that is how a consensus of browser manufacturers (MSIE, Mozilla,
Opera, etc) agree on: a script should not be able to close a window
which was not opened by javascript. Would you imagine a tv content
provider (CBS, NBC or ABC) being able to close your tv set?

I
thought the line:

if ((g_win1) && !(g_win1.closed))

would evaluate to false if g_win is undefined and would not throw an
error. Is there any way I can accomplish this? I know that I can use
code to open the window just to close it, but that seems very clumsy
to me, especially since there are up to 7 child windows

7 different child windows or 7 different documents into the same
reusable, recyclable secondary window: there is a huge difference of
meaning and consequently usability repercussion here.

that can be
opened from this parent page. Any ideas?

Hal

It would have been easier for everyone from the beginning to mention an
url.

DU
 
H

Hal

Got it to work. Thank you DU! It's a beautiful thing!

7 different child windows or 7 different documents into the same
reusable, recyclable secondary window: there is a huge difference of
meaning and consequently usability repercussion here.

7 different child windows.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top