Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to close parent window from child (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..

like image 298
Tushar Ahirrao Avatar asked Dec 22 '22 06:12

Tushar Ahirrao


2 Answers

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.

like image 98
Dennis Avatar answered Dec 23 '22 19:12

Dennis


You might want to try this

window.opener.close()
like image 26
Sajin MS Avatar answered Dec 23 '22 19:12

Sajin MS