PDA

View Full Version : Child windows not closing after refreshing parent page



mohdanis2011
11-30-2010, 03:33 AM
I have written this code for close all child window with parent window but after refreshing the parent window child window are not closing. Code bellow.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>window manager</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript">
var windows = [];
function conf() {
var rand_no_c = Math.random();
var con = rand_no_c.toString();
var_win = 'child'+con.substring(2);
windows[var_win] = window.open('test2.html',var_win,'width=400,height=200,left=50,top=20,status=0');
}

function closewindow() {
for(w in windows) {
if(!windows[w].closed) {
windows[w].close();
}
}
}
</script>

<style type="text/css">
* {margin:0;padding:0;}
</style>

</head>
<body>
<div>
<a href="javascript:" onclick="conf();">open child window</a>||
<a href="javascript:" onclick="conf();">open child window</a>||
<a href="javascript:" onclick="conf();">open child window</a>||
<a href="javascript:" onclick="conf();">open child window</a>||
<a href="test2.html">Click</a>
<button type="button" onclick="closewindow();">close all windows</button>
</div>
</body>
</html>

jscheuer1
11-30-2010, 04:15 AM
With windows there are no parents and children. Those terms are for elements on a single page.

For distinct windows you have opener (somewhat like parent), and the variable name (if any) you assigned to the opened window when you opened it.

In any case, once you do a refresh of the opener you lose all reference from it to any page that it had opened, period.

In most browsers though - you can close the opened window when the opener is refreshed. That might not be an attractive option, but it's really about all you have, so you might be able to get it to work for you.

If none of this seems adequate, something server side might be better. I'd also ask myself if a new window was even necessary. They can often be blocked by the browser, so should never be used for mission critical data. Most new windows used -say for e-commerce, have their own close button on them and are only used for things like detailed images of the product.