简体   繁体   English

从另一个窗口关闭所有子窗口的Windows浏览器

[英]Close all children windows browser from another window

This is my scenario: I've a tab in my browser (IE8) and if I click on a link, I open a new window (window's name: " Win2 "). 这是我的情况:我的浏览器(IE8)中有一个选项卡,如果单击链接,则会打开一个新窗口(窗口名称:“ Win2 ”)。 Now, i came back to the first window, open a new tab in my browser, go to another page and click to a different link. 现在,我回到第一个窗口,在浏览器中打开一个新选项卡,转到另一个页面并单击另一个链接。 Once click on the last link, I want to close the page " Win2 ". 单击最后一个链接后,我要关闭“ Win2 ”页面。 Can I do it with Javascript? 我可以用Java语言吗? How I can acces to the 1st-page's children from another page? 如何从另一页访问第一页的子级? Backend-side, I use Java 1.5 with framework struts. 后端,我将Java 1.5与框架struts一起使用。

Save the return value of every call to window.open: 保存每次对window.open的调用的返回值:

var windows = {};

windows.Win2 = window.open("...", "Win2");

To close all of them: 要关闭所有它们:

for (var name in windows) {
    if (windows.hasOwnProperty(name)) {
        windows[name].close();
        windows[name] = null;
    }
}

Or to close one: 或关闭一个:

windows.Win2.close();

You can store the opened window in a variable in your parent window. 您可以将打开的窗口存储在父窗口的变量中。

var Win2 = window.open("", "Win2");

Then you can close it later on using the variable. 然后,您可以稍后使用变量将其关闭。

Win2.close();

If you want to close Win2 from a different child window of the original parent. 如果要从原始父窗口的另一个子窗口关闭Win2。 Create a function on your parent window, then call it from the child. 在父窗口上创建一个函数,然后从子窗口中调用它。

Parent Window 父窗口

function CloseWin2() {
    Win2.close();
}

Child Window 子窗口

if(window.opener) {
    window.opener.CloseWin2();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM