close all child windows when close the main window

J

jrefactors

From the main page, there are lots of links to open new window.
i.e. <a href="#" onClick="window.open('child1.html')">Open Child
Window</a>

If I close the main page, I want to close all child windows also. That
means I
need to keep track all handles of the child windows whenever I open a
new
window. Correct? any suggestions on the approach?
Please advise. thanks!!
 
P

porneL

If I close the main page, I want to close all child windows also. That
means I need to keep track all handles of the child windows whenever I
open a
new window.

That won't work if user browses in main window.

IMO the best way is to simply add 'dependent=yes' to popup window
attributes.
Doesn't work everywhere, but when works - it works :)

BTW: never use href="#", put real address there and use return false in
onclick.
This will improve search engine visibility, ensure that windows open in new
window, links can be copied/bookmarked, etc.
 
C

Chung Leong

i.e. <a href="#" onClick="window.open('child1.html')">Open Child
Window</a>

If I close the main page, I want to close all child windows also. That
means I
need to keep track all handles of the child windows whenever I open a
new
window. Correct? any suggestions on the approach?
Please advise. thanks!!

Yes. Wouldn't be hard if you define a Javascript function for opening
windows and save the handles into an array.

Or you could use the ShowModelessDialog() function. Modless dialog boxes
stay in front of their parent window and automatically would close when the
parent closes.
 
R

Randy Webb

Chung said:
Yes. Wouldn't be hard if you define a Javascript function for opening
windows and save the handles into an array.

Or you could use the ShowModelessDialog() function. Modless dialog boxes
stay in front of their parent window and automatically would close when the
parent closes.

Modeless Dialogs are also IE-only.

I might be able to understand the OT if this were not posted to
comp.lang.javascript
 
C

Chung Leong

Randy Webb said:
Modeless Dialogs are also IE-only.

I might be able to understand the OT if this were not posted to
comp.lang.javascript

Oops. Why did the OP cross post into comp.lang.php?
 
Joined
Aug 7, 2008
Messages
2
Reaction score
0
close all child windows

Main.html

<html>
<head>
<script>
var wnd = new Array();
function openit(id)
{
if(!wnd[id] || wnd[id].closed)
wnd[id] = window.open("Child.html");
else
wnd[id].focus();
}

function closeEverything()
{
for(var i = 0; i < wnd.length; ++i)
wnd.close();
}
</script>
</head>
<body>
<form>
<input type="button" value="Open1" onclick="openit(0);" /><br/>
<input type="button" value="Open2" onclick="openit(1);" /><br/>
<input type="button" value="Open3" onclick="openit(2);" /><br/>
</form>
</body>
</html>

Child.html

<html>
<head>
<script>
function closeAll()
{
window.opener.closeEverything();
}
</script>
</head>
<body>
<form>
<input type="button" value="Close Me" onclick="window.close();" /><br/><br /><br />
<input type="button" value="Close all children" onclick="closeAll();" /><br/>
</form>
</body>
</html> Main.html

<html>
<head>
<script>
var wnd = new Array();
function openit(id)
{
if(!wnd[id] || wnd[id].closed)
wnd[id] = window.open("Child.html");
else
wnd[id].focus();
}

function closeEverything()
{
for(var i = 0; i < wnd.length; ++i)
wnd.close();
}
</script>
</head>
<body>
<form>
<input type="button" value="Open1" onclick="openit(0);" /><br/>
<input type="button" value="Open2" onclick="openit(1);" /><br/>
<input type="button" value="Open3" onclick="openit(2);" /><br/>
</form>
</body>
</html>

<strong class="highlight">Child</strong>.html

<html>
<head>
<script>
function closeAll()
{
window.opener.closeEverything();
}
</script>
</head>
<body>
<form>
<input type="button" value="Close Me" onclick="window.close();" /><br/><br /><br />
<input type="button" value="Close all children" onclick="closeAll();" /><br/>
</form>
</body>
</html>
 
Joined
Aug 7, 2008
Messages
2
Reaction score
0
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>untitled</title>
<style type="text/css">

input {
width: 80px;
font-size: 75%;
background: beige;
padding: 2px;
margin: 2px;
}

</style>
<script type="text/javascript">

function popit(n)
{
var w, wname = 'pop' + n;
var l = 100 * n + 100;
var t = 100 * n + 100;
w = window.open('javascript:"<h4>'+wname+'</h4>"',
wname,
'width=200,height=200,left='+l+',top='+t+',status= 0');
addName(wname);
return false;
}

function addName(wname)
{
self.name += (wname + '@');
}

function closeByName(wname)
{
window.open('javascript:"<script>window.close()<\/script>"', wname);
}

function closeAll()
{
var wname,
wcoll = self.name.split('@');
self.name = '';
for (var i = 0, l = wcoll.length; i < l; ++i)
if (wname = wcoll)
closeByName(wname);
return false;
}

</script>
</head>
<body>
<form>
<input id="w1" type="button"
value="open pop 1"
onclick="popit(1)" />
<br />
<input id="w2" type="button"
value="open pop 2"
onclick="popit(2)" />
<br />
<input id="w3" type="button"
value="open pop 3"
onclick="popit(3)" />
<br /><br />
<input id="ca" type="button"
value="close all"
onclick="closeAll()" />
</form>
</body>
</html>
 

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

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top