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

发布于 2024-12-16 01:08:40 字数 889 浏览 0 评论 0原文

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

我的孩子 html :

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


        }

    </script>
    <body>
        <div onclick="closeAll();" onunload="window.opener.close();">
            Close Parent and self
        </div>
    </body>

请帮助我..

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

拥有 2024-12-23 01:08:40

来自 window.close() MDN

只有打开的窗口才允许调用该方法
通过使用 window.open 方法的脚本。如果窗户没有打开
通过脚本,JavaScript 控制台中出现以下错误:
脚本可能无法关闭不是由脚本打开的窗口。

您可以从父窗口关闭子窗口,但反之则不行。

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.

彻夜缠绵 2024-12-23 01:08:40

你可能想尝试这个

window.opener.close()

You might want to try this

window.opener.close()
栖竹 2024-12-23 01:08:40
function closeW() {
   window.open('javascript:window.open("", "_self", "");
   window.close();', '_self');
}

从子页面调用上面的函数并设置超时。它适用于 IE,有时也适用于 Chrome。

setTimeout(closeW, 500);

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);

计㈡愣 2024-12-23 01:08:40

更新答案
让孩子靠近父母是不好的耦合。我原来的答案之所以有效,是因为我的父窗口本身是由祖父母窗口通过 javascript 打开的!

为了避免不必要的耦合,正确的方法是让父窗口使用 setInterval 轮询子窗口,以获取触发关闭的事件。在此示例中,该事件是子窗口关闭。我实际上用它来用ajax刷新部分页面而不是关闭窗口,但原理是相同的......

        $('#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; 
        } );

原始答案
实际上有一个相当简单的解决方法。

创建一个除了自行关闭之外不执行任何操作的页面,例如 close.html

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

然后在子窗口中,将打开程序重定向到自行关闭的页面:

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

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';
白龙吟 2024-12-23 01:08:40

如果您的意思是父框架内的子框架,则使用

window.parent.close();

如果您的意思是打开的窗口(使用 window.open()),则尝试此

window.opener.close();

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();
ゝ杯具 2024-12-23 01:08:40

有一个可行的解决方案(至少在 Chrome 中):

这是父页面的代码:

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

这是子页面的代码:

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

试试吧!

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!

与之呼应 2024-12-23 01:08:40
function closeAll()
{
    window.opener.justClose();
}
function closeAll()
{
    window.opener.justClose();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文