是否可以关闭父窗口从孩子(Javascript)? [英] Is it possible to close parent window from child (Javascript)?

查看:106
本文介绍了是否可以关闭父窗口从孩子(Javascript)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果你有任何的话,请分享我的代码。

>

我的父html:

 < script> 
var winr;
var selfw;
function openAn(){
selfw = self.window;
winr = window.open(shell.htm,);

}

函数justClose(){
// setTimeout(closeAfterMin(),2000);

winr.close();
}

function closeAfterMin(){
alert(sd);
selfw.close();
}

< / script>
< body onload ='openAn()'>
< h1>新的Web项目页面< / h1>
< / body>

我的孩子html:

 < script> 
function closeAll(){
window.parent.close();


}

< / script>
< body>
< div onclick =closeAll(); onunload的= window.opener.close(); >
关闭Parent和self
< / div>
< / body>

请帮助我..

解决方案

window.close()MDN


只有通过使用window.open方法的脚本打开
才能调用此方法。如果窗口未通过脚本打开
,JavaScript控制台中将显示以下错误:
脚本可能无法关闭脚本未打开的窗口。




您可以从父母关闭子窗口,但不能相反。


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..

解决方案

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.

You can close the child window from the parent, but not the other way around.

这篇关于是否可以关闭父窗口从孩子(Javascript)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆