gpt4 book ai didi

javascript - 是否可以从子窗口(Javascript)关闭父窗口?

转载 作者:搜寻专家 更新时间:2023-10-31 22:22:47 24 4
gpt4 key购买 nike

我正在尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com