Javascript:关闭弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9170259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:49:14  来源:igfitidea点击:

Javascript: Close pop up window

javascriptpopup

提问by Thai Tran

I have a button on the main page, which will open (window.open()) a window W1 to allow user to select stuff on it. After that, user press OK button on the W1 to open window W2 (again window.open()). How can i close the W1 after the user press OK?

我在主页上有一个按钮,它将打开 (window.open()) 一个窗口 W1 以允许用户在其上选择内容。之后,用户按下 W1 上的 OK 按钮打开窗口 W2(再次 window.open())。用户按 OK 后如何关闭 W1?

回答by Aaron

Use the window.close()method with the name of the target window as shown below:

使用window.close()具有目标窗口名称的方法,如下所示:

win1 = window.open("","","width=100,height=100");
okBtn.onclick = function() {
    win2 = window.open("","","width=100,height=100");
    win1.close();
}

回答by Fancy John

On the main page you save popup into W1 and define a function that will close W1:

在主页面上,您将弹出窗口保存到 W1 中并定义一个将关闭 W1 的函数:

W1 = window.open("","","width=100,height=100");

function closeW1() {
    W1.close();
}

Now on W1 in the same place where you open W2:

现在在 W1 上打开 W2 的同一个地方:

okBtn.onclick = function() {
    W2 = window.open("","","width=100,height=100");
    window.opener.closeW1();
}

That's it. You're done.

而已。你完成了。