javascript中如何在关闭父窗口时关闭子窗口?

发布于 2024-09-25 03:48:32 字数 33 浏览 4 评论 0原文

javascript中如何在关闭父窗口时关闭子窗口?

How to close the child window when closing parent window in javascript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人海汹涌 2024-10-02 03:48:32

如果父窗口和子窗口指的是弹出窗口和打开它的窗口,则不能完全执行此操作,但主窗口中的以下脚本将在主窗口卸载时关闭弹出窗口(即关闭或用户导航到另一个页面或刷新):

var popUp = window.open("popup.html", "popup", "width=300,height=200");

window.onunload = function() {
    if (popUp && !popUp.closed) {
        popUp.close();
    }
};

历史记录:过去可以在某些浏览器(例如 Firefox)中通过使用第三个参数中的 dependent 窗口属性来执行此操作window.open 调用(例如 var popUp = window.open("popup.html", "popup", "width=300,height=200,dependent");) 但根据 quirksmode 它不再受任何支持。我自己还没有测试过这个。

If by parent and child windows you mean a pop-up window and the window that opened it, you can't do exactly this, but the following script in the main window will close the pop-up window when the main window is unloaded (i.e. either closed or the user navigates to another page or refreshes):

var popUp = window.open("popup.html", "popup", "width=300,height=200");

window.onunload = function() {
    if (popUp && !popUp.closed) {
        popUp.close();
    }
};

Historical note: it used to be possible to do this in some browsers (e.g. Firefox) by using the dependent window property in the third parameter of the window.open call (e.g. var popUp = window.open("popup.html", "popup", "width=300,height=200,dependent");) but according to quirksmode it's no longer supported in anything. I haven't tested this myself.

半岛未凉 2024-10-02 03:48:32

当从一个窗口打开多个子窗口并且您希望在父窗口关闭时关闭所有子窗口时,您需要将子窗口引用存储在数组中。在窗口的 onunload 事件上,您可以迭代此数组并对每个子窗口引用调用 close() 方法。要查看完整示例请单击此处

window.onunload = function closeChildWin(){
 for(var i=0; i<childwindows.length; i++){
  try{
     childwindows[i].close()
  }catch(e){
   alert(e);
  }
 }
}

When multiple child windows are opened form one window and you want to close all of them when parent window is closed, then you need to store child window references in array. On window's onunload event you can iterate this array and call close() method on each child window reference. To see complete example click here

window.onunload = function closeChildWin(){
 for(var i=0; i<childwindows.length; i++){
  try{
     childwindows[i].close()
  }catch(e){
   alert(e);
  }
 }
}
梦在夏天 2024-10-02 03:48:32

我在从弹出窗口内创建弹出窗口时遇到了这个问题。创建窗口名称后访问它似乎没有任何作用。

首先创建一个窗口:

popup = window.open(address, name, params)

首先我试图这样做:

window.onbeforeunload = function () {
    popup.close()
}

但问题是名称“popup”在创建后无法访问。我没有得到任何回应。

这就是我让它发挥作用的方式。在弹出窗口的 javascript 中,我添加了:

window.parent.onbeforeunload = function () {
    window.close()
}

现在,每当父窗口关闭时,所有“子”窗口也会关闭。即使弹出窗口下有弹出窗口。只要每个“弹出”窗口都有此窗口,当其“上方”的任何窗口关闭时,它就会关闭。

I ran into this problem while creating popups from within popups. Accessing a windows name after it's been created seemed to do nothing.

First a window is created:

popup = window.open(address, name, params)

First I was trying to do this:

window.onbeforeunload = function () {
    popup.close()
}

But the problem is that the name 'popup' couldn't be accessed after it was created. I was getting no response.

So this is how I got it working. Inside the javascript for the popup window I added:

window.parent.onbeforeunload = function () {
    window.close()
}

Now, whenever the parent window is close, all "child" windows are closed as well. even if there are popups under popups under popups. As long as each "popup" window has this, it closes when any window "above" it closes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文