我们在编写页面过程中,可能会通过 window.open 方法来打开多个子窗口。这样一来,在想关闭窗口的时候只能一个个的关闭所有打开的窗口,太烦了。那么有没有简单的办法,使得在关闭主窗口的时候,将所有附属的子窗口也一起关闭呢?嗯,既然写这篇BLOG了,答案肯定是有了哈。但不一定是很好的方法,只是实现了这个功能而已。其实,也没什么,只是在OPEN打开一个窗口时,使用一个数组记住了它的句柄。
上代码,如下,应该是一下就懂的,呵呵:
<html>
<head>
<title>IE关闭主窗口时,同时关闭所有的子窗口</title>
<SCRIPT language=javascript>
// 声明一个数组来记录所有打开的子窗口
var allChild = new Array();
// 打开三个子窗口,并记录下它们的句柄
var child = window.open("./window_open_test2.html","_blank");
allChild.push(child);
child = window.open("./window_open_test2.html","_blank");
allChild.push(child);
child = window.open("./window_open_test2.html","_blank");
allChild.push(child);
// 关闭自己,同时关闭所有子窗口
function closeAll() {
var tmp;
// undefined 不能加引号,即不可写成 "undefined"
while((tmp = allChild.pop()) != undefined) {
tmp.close();
}
window.close();
}
</SCRIPT>
<head>
<body>
<input type="button" value="close" onclick="closeAll();" />
</body>
</html>
?
?