By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,656 Members | 1,327 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,656 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
hf******@cfl.rr.com (Hal) writes:
Can someone please tell me what is wrong with this script? <script language="JavaScript">
Should be:
<script type="text/javascript">
The "type" attribute is required in HTML 4 and is always sufficient.
<!--
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2

P: n/a
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
Jul 23 '05 #3

P: n/a
DU
Hal wrote:
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/interac...default-script

<head>
....
<meta http-equiv="Content-Script-Type" content="text/javascript">
....
</head>
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
Jul 23 '05 #4

P: n/a
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.
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.