是否可以从子窗口(Javascript)关闭父窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8121755/
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
Is it possible to close parent window from child (Javascript)?
提问by Tushar Ahirrao
I am trying to close parent window from child window using javascript, but its not going to work.
我正在尝试使用 javascript 从子窗口关闭父窗口,但它不起作用。
Please share me code if you have any.
如果你有任何代码,请分享我。
My parent html :
我的父母 html :
<script>
var winr;
var selfw;
function openAn(){
selfw=self.window;
winr=window.open("shell.htm","");
}
function justClose(){
//setTimeout("closeAfterMin()",2000);
winr.close();
}
function closeAfterMin(){
alert("sd");
selfw.close();
}
</script>
<body onload='openAn()' >
<h1>New Web Project Page</h1>
</body>
My Child html :
我的孩子 html :
<script>
function closeAll(){
window.parent.close();
}
</script>
<body>
<div onclick="closeAll();" onunload="window.opener.close();">
Close Parent and self
</div>
</body>
Please help me..
请帮我..
回答by Dennis
From window.close() MDN:
This method is only allowed to be called for windows that were opened by a script using the window.open method. If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script.
仅允许为使用 window.open 方法由脚本打开的窗口调用此方法。如果窗口不是由脚本打开的,则 JavaScript 控制台中会出现以下错误:脚本可能不会关闭不是由脚本打开的窗口。
You can close the child window from the parent, but not the other way around.
您可以从父窗口关闭子窗口,但不能反过来。
回答by Reny Mathew
function closeW() {
window.open('javascript:window.open("", "_self", "");
window.close();', '_self');
}
call the function above with a timeout from child page. It works in IE and sometimes in Chrome too.
从子页面超时调用上面的函数。它适用于 IE,有时也适用于 Chrome。
setTimeout(closeW, 500);
setTimeout(closeW, 500);
回答by AntonChanning
Updated AnswerHaving a child close a parent is bad coupling. My original answer only worked because my parent window was itself opened via javascript by a grandparent window!
更新的答案让孩子靠近父母是不好的耦合。我的原始答案仅有效,因为我的父窗口本身是由祖父窗口通过 javascript 打开的!
The correct way to do this, to avoid uneccessary coupling, is to have the parent window poll the child, using setInterval
, for the event that triggers the close. In this example that event is the child window closing. I actually use this to refresh part of the page with ajax rather than closing the window, but the principle is the same...
为避免不必要的耦合,正确的方法是让父窗口轮询子窗口,使用setInterval
, 以获取触发关闭的事件。在此示例中,该事件是子窗口关闭。我其实是用这个用ajax刷新部分页面而不是关闭窗口,但是原理是一样的...
$('#open_child').click(
function(e) {
e.preventDefault();
docs_popup = window.open($('#open_child').attr('href'),'upload_doc');
var timer = setInterval(function() {
if(docs_popup.closed) {
window.close();
}
}, 1000);
return false;
} );
Original AnswerThere is actually a fairly simple work around for this.
原始答案实际上有一个相当简单的解决方法。
Create a page that does nothing but close itself, for example close.html
:
创建一个只关闭自身的页面,例如close.html
:
<!DOCTYPE html>
<html>
<head>
<script>window.close();</script>
</head>
<body>
<p>This window should close automatically.</p>
</body>
</html>
Then in the child window, redirect the opener to the page that closes itself:
然后在子窗口中,将开启器重定向到自行关闭的页面:
window.opener.location.href='close.html';
回答by Sajin MS
You might want to try this
你可能想试试这个
window.opener.close()
回答by Royi Namir
function closeAll()
{
window.opener.justClose();
}
回答by Willem Mulder
If you mean a child-frame within a parent-frame, then use
如果您的意思是父框架中的子框架,则使用
window.parent.close();
If you mean an opened window (with window.open()), then try this
如果你的意思是一个打开的窗口(使用 window.open()),那么试试这个
window.opener.close();
回答by Rodolphe
There is a working solution (in Chrome, at least):
有一个可行的解决方案(至少在 Chrome 中):
Here's the code for parent page:
这是父页面的代码:
<html>
<head>
<script type="text/javascript">
function openChild() {
var w = window.open("parent_close_child.html", "child");
}
</script>
</head>
<body>
<input type="button" value="Open" onclick="openChild()" />
</body>
</html>
Here's the code for child page:
这是子页面的代码:
<html>
<head>
<script type="text/javascript">
function closeParent() {
var w = window.opener;
window.opener = self;
w.close();
}
</script>
</head>
<body>
<input type="button" value="Close parent" onclick="closeParent()" />
</body>
</html>
Just try it!
就试一试吧!