您的位置:首页 > 其它

【翻译】父窗口关闭的时候子窗口也同时关闭

2008-07-08 13:19 218 查看
如何当父窗口关闭的时候子窗口也同时关闭呢?假如我是网站的使用者,我倒是不想父窗口关闭时会自动关闭所有的子窗口,因此我觉得上面的这个需求是奇怪的。

下面就是可以实现如标题所描述的代码。我想确保的是我所有已经打开的窗口之间的联系,然后当父窗口被关闭的时候,简单地使用一个循环这些子窗口,并关闭它们。

在父窗口的 body 标签内添加 "onunload" 事件。请注意当页面被刷新的时候会引发该事件。

<body onunload="closeChildWindows()">

<a href="#" onclick="openWindow('Pop1.aspx','Pop1')">Open Window 1</a>

<a href="#" onclick="openWindow('Pop2.aspx','Pop2')">Open Window 2</a>

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

// create an array of child windows

var childWindows = new Array();

function openWindow(url,name)

{

newWindow = window.open(url,name,'width=100,height=100;dependent=yes');

// add the new child window in the collection

childWindows.push(newWindow);

}

function closeChildWindows()

{

if(!sure) return;

// iterate through the collection and close all the windows

for(i=0; i<childWindows.length; i++)

{

childWindows[i].close();

}

}

你还可以使用一个确认框来确定是否要关闭子窗口。提醒用户所有他/她可以选择是否将打开的子窗口都被关闭。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐