search

Home  >  Q&A  >  body text

javascript - window.opener.close() has no effect

1. Parent page window.open() opens a new page

var targetWeb=null;
if(targetWeb){
    targetWeb.focus();
}else{
    targetWeb=window.open('https://segmentfault.com','segmentfault');
}

2. Close the parent page in the child page

window.opener.close();

If it is found that the child page cannot close the parent page, it will prompt: Scripts may close only the windows that were opened by it
But if it is replaced with: window.opener.location.href='https://www.hao123. com' but it works, what is the reason

PHP中文网PHP中文网2714 days ago1231

reply all(3)I'll reply

  • phpcn_u1582

    phpcn_u15822017-06-26 10:54:27

    reply
    0
  • 大家讲道理

    大家讲道理2017-06-26 10:54:27

    The close method can only close the window opened by itself

    reply
    0
  • 黄舟

    黄舟2017-06-26 10:54:27

    <html>
    <body>

    <script type="text/javascript">
    myWindow=window.open('','','width=200,height=100');
    myWindow.document.write("This is 'myWindow'");
    myWindow.document.write("<script>window.opener.close()</script>");
    myWindow.focus();
    myWindow.opener.document.write("This is the parent window");
    myWindow.close();
    </script>

    </body>
    </html>

    
    尝试了这段代码,在父窗口中直接使用
    
     myWindow=window.open('','','width=200,height=100');
        myWindow.close();
    

    可以关掉子窗口,在子窗口中无效。

    reply
    0
  • Cancelreply