最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Is it possible to close parent window from child (Javascript)? - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Nov 14, 2011 at 12:58 Tushar Ahirrao asked Nov 14, 2011 at 12:42 Tushar AhirraoTushar Ahirrao 13.1k18 gold badges66 silver badges98 bronze badges 6
  • Do you use iframe inside a parent window? And you want to close the parent window inside the iframe? – steveyang Commented Nov 14, 2011 at 12:48
  • no i have not used any iframe inside a parent window – Tushar Ahirrao Commented Nov 14, 2011 at 12:49
  • Can you include the code you're using and/or a JSFiddle? The solutions listed already seem like they should work, yet they're not working for you. – jbrookover Commented Nov 14, 2011 at 12:53
  • JavaScript can only close windows that it opened. Since your parent window has most likely not been opened by JavaScript, it can't be closed by JavaScript. – RoToRa Commented Nov 14, 2011 at 13:06
  • @TusharAhirrao You don't use an IFRAME element? – Šime Vidas Commented Nov 14, 2011 at 13:09
 |  Show 1 more comment

7 Answers 7

Reset to default 8

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.

You might want to try this

window.opener.close()
function closeW() {
   window.open('javascript:window.open("", "_self", "");
   window.close();', '_self');
}

call the function above with a timeout from child page. It works in IE and sometimes in Chrome too.

setTimeout(closeW, 500);

Updated Answer Having a child close a parent is bad coupling. My original answer only worked because my parent window was itself opened via javascript by a grandparent window!

The correct way to do this, to avoid uneccessary coupling, is to have the parent window poll the child, using setInterval, for the event that triggers the close. In this example that event is the child window closing. I actually use this to refresh part of the page with ajax rather than closing the window, but the principle is the same...

        $('#open_child').click( 
        function(e) {
          e.preventDefault(); 
          docs_popup = window.open($('#open_child').attr('href'),'upload_doc');

          var timer = setInterval(function() {   
              if(docs_popup.closed) {  
                  window.close();
              }  
          }, 1000); 

          return false; 
        } );

Original Answer There is actually a fairly simple work around for this.

Create a page that does nothing but close itself, for example close.html:

<!DOCTYPE html>
<html>
    <head>
        <script>window.close();</script>
    </head>
    <body>
        <p>This window should close automatically.</p>
    </body>
</html>

Then in the child window, redirect the opener to the page that closes itself:

window.opener.location.href='close.html';

If you mean a child-frame within a parent-frame, then use

window.parent.close();

If you mean an opened window (with window.open()), then try this

window.opener.close();

There is a working solution (in Chrome, at least):

Here's the code for parent page:

<html>
<head>
<script type="text/javascript">

function openChild() {
    var w = window.open("parent_close_child.html", "child");
}

</script>
</head>
<body>

<input type="button" value="Open" onclick="openChild()" />

</body>
</html>

Here's the code for child page:

<html>
<head>
<script type="text/javascript">

function closeParent() {
    var w = window.opener;
    window.opener = self;
    w.close();
}

</script>
</head>
<body>

<input type="button" value="Close parent" onclick="closeParent()" />

</body>
</html>

Just try it!

function closeAll()
{
    window.opener.justClose();
}
发布评论

评论列表(0)

  1. 暂无评论