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

标签 javascript jquery html dom

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

相关文章:

javascript - 在 Kendo UI Grid 中重置重新排序的列

jquery - $(window).unload 未触发

html - Material 设计精简版在我的网站上不起作用

html - 使用背景属性打开.AI文件

javascript - 如何在主 div 中的每个新段落上制作一个编辑按钮。 JavaScript

javascript - 调度 CustomEvent 始终返回 true

javascript - 为什么这张图片在发送 AJAX 请求之前不显示并在完成时隐藏?

javascript - 使用 jquery 移动点击而不是点击

javascript - Ember 使用点击、命令/控制和 shift 键从列表中选择

jquery - 列表样式问题单击此 jQuery