0

I am attempting to close a child window, but have encountered some extremely bizarre behavior in webkit-based browsers - it doesn't work! Here's the HTML snippet of the parent:

var winBrowser = null;

function openWindow(strAction) {
    switch(strAction) {
        case "req":
           winBrowser = window.open(...);
           ajax(...);
           break;
        case "succ":
           // window.open(...);
           break;
        case "fail":
           winBrowser.close();
           // winBrowser.closeMe();
           break;
    }
}

The function is initially called with 'req' as the strAction value. The 'ajax' call in that segment calls the same function again but with a 'succ' or 'fail' parameter depending on the result from the ajax call itself. At first I attempted to simply open the child window upon success, but this failed in webkit-based browsers. So I adjusted the code to load a blank window in the 'req' section and close it if the ajax call failed - as a work around. However, the close(); call doesn't work either. Any thoughts on a resolution?

UPDATE:

I've also attempted on calling a javascript function in the child window that simply attempts to close the child window:

function closeMe(){ window.close(); }
user1646428
  • 179
  • 2
  • 12
  • FWIW, the reason it didn't open within the callback of the ajax call is that that event is not trusted, ie it wasn't initiated by the user. You can't open new windows in non-trusted events. Maybe you can't close them either, I've never tried... – James Thorpe Jan 06 '16 at 17:10
  • That's interesting. Is there a way to get around this? – user1646428 Jan 06 '16 at 17:11
  • No, and thank goodness for that. Modern browsers would be fixed quickly if some workaround were found. – Pointy Jan 06 '16 at 17:13
  • Well what's interesting is that FF doesn't have this restriction in the original code (which is commented out). Any thoughts on how to accomplish what I'm trying to do? – user1646428 Jan 06 '16 at 17:13
  • Well what you're trying to do makes sense, but there's not enough information really to help - how exactly does it fail? Is an error reported in the console? Are you sure that the code makes it to that statement? – Pointy Jan 06 '16 at 17:15
  • Unfortunately I can't find that information out either. As stated, I don't have problems in FF. The webkit-based browser I'm using is Rekonq, but it doesn't report any problems. But it also doesn't work. I'll update the question to show alternative approaches I've tried. – user1646428 Jan 06 '16 at 17:16
  • For starters, a `console.log()` call when your code is about to try calling `winBrowser.close()` would let you confirm that it's getting to that point. – Pointy Jan 06 '16 at 17:18
  • I've made sure it is being called by surrounding it with alert() calls. I know that segment is getting accessed. – user1646428 Jan 06 '16 at 17:20
  • See also http://stackoverflow.com/questions/19761241/window-close-and-self-close-do-not-close-the-window-in-chrome – John Hascall Jan 06 '16 at 17:41
  • @JohnHascall thanks for the link. So it basically looks like javascript can't close windows any more or did I misread this? The only loophole that I can see if that if the same script that was called to open the window can close it. If that's the case I don't understand why my code isn't working... – user1646428 Jan 06 '16 at 17:55
  • In can, but not in situations where it could be unsafe. – John Hascall Jan 06 '16 at 17:56
  • What situations are those? Because if the only allowable example is a user-initiated click, that's pretty silly since it could be accomplished by just clicking the 'X' on the window itself. – user1646428 Jan 06 '16 at 17:57
  • Is there a way to have the child window be on a timeout close? I've tried putting something like "setTimeout(window.close(), 2000);" in the HTML of the child, but that doesn't seem to work either. Perhaps a webkit override to allow windows to be closed? – user1646428 Jan 06 '16 at 18:46

0 Answers0