打开弹出窗口后如何关闭父窗口
问题描述:
这是关于在线考试..
使用Javascript我可以从下拉列表中选择域名后打开一个弹出窗口进行测试(.net,来自父窗口的java,oracle)。
This is regarding online exam..
Using Javascript i am able to open a popup window for taking test after selecting the domain from the dropdownlist (.net,java,oracle) from parent window.
function basicPopup(url)
{
popupWindow = window.open(url, 'popUpWindow',
'height=500,width=500,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no, status=yes');
}
但是如何在打开弹出窗口后关闭父窗口。
But how to close the parent window after a popup window is opened.
答
正如 Sergey Alexandrovich Kryukov 所说,这是一个坏主意。因此,重新考虑设计。
我仍然想给你这个概念。
参考 - window.close [ ^ ]。
关闭打开/子窗口
因为你已经分配了窗口
到变量popupWindow
,因此您只能在该变量的帮助下关闭窗口。
我们可以说popupWindow
是一个对象
,这是使用open
方法初始化的window
类的实例。
使用以下代码关闭
窗口。
As Sergey Alexandrovich Kryukov suggested, it is a bad idea. So, reconsider the design.
Still I would like to give the concept to you.
Refer - window.close[^].
To close the opened/child window
As you have assignedwindow
to the variablepopupWindow
, so you can close the window with the help of that variable only.
We can say thatpopupWindow
is anobject
, which is a instance ofwindow
class initialized usingopen
method.
Use the following code toclose
the window.
popupWindow.close();
关闭父窗口
由 Sergey Alexandrovich Kryukov 引用 Solution 2 。
解决方案1的重要补充:
如何获得第一个窗口的引用,即你开始使用的窗口?您可以使用该属性opener
:
http://www.w3schools.com/jsref/obj_window.asp [ ^ ],
http://www.w3schools.com/ jsref / prop_win_opener.asp [ ^ ]。
但是执行window.opener.close()
是一件非常糟糕的事情。用户不希望你这么讨厌。他们没有保持这个窗口的链接,他们确信他们总能回到这个窗口,但是他们无法通过后退按钮回来。一点都不好。我告诉你,检查你的UI设计。-SA
An important addition to Solution 1:
How to get the reference to the first window, the one you started with? You can use the propertyopener
:
http://www.w3schools.com/jsref/obj_window.asp[^],
http://www.w3schools.com/jsref/prop_win_opener.asp[^].
But doingwindow.opener.close()
is a really bad thing. The users don't expect such a nasty move from you. They don't keep a link to this window, they are sure they can always come back to this windows, but they won't be able to, they won't be able to come back by "Back" button. Not nice at all. I tell you, review your UI design.—SA
<!DOCTYPE html>
<html>
<body>
<p>Click the button</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
window.open("http://www.google.co.in", "", "width=1300, height=700");
window.parent.close();
}
</script>
</body>
</html>