是否可以从子窗口关闭父窗口(Javascript)?
我正在尝试使用 javascript 从子窗口关闭父窗口,但它不起作用。
如果您有代码,请分享给我。
我的父母 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>
我的孩子 html :
<script>
function closeAll(){
window.parent.close();
}
</script>
<body>
<div onclick="closeAll();" onunload="window.opener.close();">
Close Parent and self
</div>
</body>
请帮助我..
I am trying to close parent window from child window using javascript, but its not going to work.
Please share me code if you have any.
My parent 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 :
<script>
function closeAll(){
window.parent.close();
}
</script>
<body>
<div onclick="closeAll();" onunload="window.opener.close();">
Close Parent and self
</div>
</body>
Please help me..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
来自 window.close() MDN:
您可以从父窗口关闭子窗口,但反之则不行。
From window.close() MDN:
You can close the child window from the parent, but not the other way around.
你可能想尝试这个
You might want to try this
从子页面调用上面的函数并设置超时。它适用于 IE,有时也适用于 Chrome。
setTimeout(closeW, 500);
call the function above with a timeout from child page. It works in IE and sometimes in Chrome too.
setTimeout(closeW, 500);
更新答案
让孩子靠近父母是不好的耦合。我原来的答案之所以有效,是因为我的父窗口本身是由祖父母窗口通过 javascript 打开的!
为了避免不必要的耦合,正确的方法是让父窗口使用
setInterval
轮询子窗口,以获取触发关闭的事件。在此示例中,该事件是子窗口关闭。我实际上用它来用ajax刷新部分页面而不是关闭窗口,但原理是相同的......原始答案
实际上有一个相当简单的解决方法。
创建一个除了自行关闭之外不执行任何操作的页面,例如
close.html
:然后在子窗口中,将打开程序重定向到自行关闭的页面:
Updated Answer
Having 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!
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...Original Answer
There is actually a fairly simple work around for this.
Create a page that does nothing but close itself, for example
close.html
:Then in the child window, redirect the opener to the page that closes itself:
如果您的意思是父框架内的子框架,则使用
如果您的意思是打开的窗口(使用 window.open()),则尝试此
If you mean a child-frame within a parent-frame, then use
If you mean an opened window (with window.open()), then try this
有一个可行的解决方案(至少在 Chrome 中):
这是父页面的代码:
这是子页面的代码:
试试吧!
There is a working solution (in Chrome, at least):
Here's the code for parent page:
Here's the code for child page:
Just try it!