window.close and self.close do not close the window in Chrome

后端 未结 16 1840
孤城傲影
孤城傲影 2020-11-21 05:48

The issue is that when I invoke window.close() or self.close() it doesn\'t close the window. Now there seems to be a belief that in Chrome you can\

相关标签:
16条回答
  • 2020-11-21 06:02

    You can also try to use my code below. This will also help you to redirect your parent window:

    Parent:

    <script language="javascript">
    
    function open_a_window() 
    {
        var w = 200;
            var h = 200;
            var left = Number((screen.width/2)-(w/2));
            var tops = Number((screen.height/2)-(h/2));
    
            window.open("window_to_close.html", '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+tops+', left='+left);
    
       return false;
    }
    
    // opener:
    window.onmessage = function (e) {
      if (e.data === 'location') {
        window.location.replace('https://www.google.com');
      }
    };
    
    </script>
    
    <input type="button" onclick="return open_a_window();" value="Open New Window/Tab" />
    

    Popup:

    <!DOCTYPE html>
    <html>
    <body onload="quitBox('quit');">
    
    <h1>The window closer:</h1>
    
    <input type="button" onclick="return quitBox('quit');" value="Close This Window/Tab" /> 
    
    
    <script language="javascript">
    
    function quitBox(cmd) 
    {      
        if (cmd=='quit')    
        {   
           window.opener.postMessage('location', '*');
           window.open(location, '_self').close();    
        }     
        return false;   
    }
    
    </script>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-21 06:03

    In tampermonkey now you can use

    // @grant        window.close
    

    And then just simply call

    window.close();
    
    0 讨论(0)
  • 2020-11-21 06:09

    I found a new way that works for me perfetly

    var win = window.open("about:blank", "_self");
    win.close();
    
    0 讨论(0)
  • 2020-11-21 06:10

    I wanted to share my "solution" with this issue. It seems like most of us are trying to close a browser window in a standard browser session, where a user is visiting some website or another, after they've visited other websites, and before they'll visit some more.

    However, my situation is that I am building a web app for a device that will essentially be the only thing the device does. You boot it up, it launches Chrome and navigates to our app. It's not even connected to the internet, any servers it talks to are on our local Docker containers. The user needs a way to close the app (aka close Chrome) so they can access the terminal to perform system updates.

    So it boots up, launches Chrome in kiosk mode, at the correct address already. This happens to easily satisfy the second option in the spec:

    A browsing context is script-closable if it is an auxiliary browsing context that was created by a script (as opposed to by an action of the user), or if it is a top-level browsing context whose session history contains only one Document.

    So simply calling window.close() from my kiosk app just works!

    0 讨论(0)
  • 2020-11-21 06:10

    Only if you open a new window using window.open() will the new window be able to close using code as I have mentioned above. This works perfectly for me :) Note : Never use href to open the page in a new tab. Window.close() does not work with "href" . Use window.open() instead.

    0 讨论(0)
  • 2020-11-21 06:11

    Despite thinking it is "patently false", what you say "seems to be a belief" is actually correct. The Mozilla documentation for window.close says

    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 say that it is "supposed to still do it" but I don't think you'll find any reference which supports that, maybe you've misremembered something?

    0 讨论(0)
提交回复
热议问题