473,616 Members | 2,973 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

<script language="JavaS cript">
<!--
function HF_CloseAllChil dren() {
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
4 2253
hf******@cfl.rr .com (Hal) writes:
Can someone please tell me what is wrong with this script? <script language="JavaS cript">
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_CloseAllChil dren() {
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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2
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(the URL,winName,fea tures);
}
</script>
The body tag of the parent page looks like this:

<body OnUnload = "javascript:HF_ CloseAllChildre n();">

Which executes this script:

<script type="text/javascript">
function HF_CloseAllChil dren() {
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
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(the URL,winName,fea tures);
}
</script>
The body tag of the parent page looks like this:

<body OnUnload = "javascript:HF_ CloseAllChildre n();">

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_CloseAllChil dren() {
if ((g_win1) && !(g_win1.closed ))
g_win1.close();
}
</script>

<script type="text/javascript">
function HF_CloseAllChil dren()
{
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_t o_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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2268
by: Alex Rast | last post by:
First of all, this is not a programming question. I'm a user, not programming in JavaScript. I'm not, however, a novice user or even a power user - I certainly know programming intimately as well as computer hardware, configuration, etc... down to the lowest level (e.g. BIOS, assembly language, etc). Anyway, I've obviously got something wrong with JavaScript configuration, files, etc. in Windows 2000, Service Pack 3, because on sites...
136
9293
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
8
3651
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
1
7143
pbmods
by: pbmods | last post by:
Looking for a print_r() for JavaScript? Look no further! Finally, a decent way to figure out what's in that mysterious Array or Object! Note that this version of print_r() relies on (included) getType(), which is designed to be a browser-independent way of detecting basic types in JavaScript (unfortunately, the $object.constructor.match() trick doesn't work in Safari nor IE). Special thanks to the following TSDN members whose help was...
0
8146
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8592
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7121
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6097
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4063
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4141
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2579
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.