我正在尝试使用 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>
我的 child html:
<script>
function closeAll(){
window.parent.close();
}
</script>
<body>
<div onclick="closeAll();" onunload="window.opener.close();">
Close Parent and self
</div>
</body>
请帮帮我..
最佳答案
来自 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.
您可以从父窗口关闭子窗口,但反之则不行。
关于javascript - 是否可以从子窗口(Javascript)关闭父窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8121755/