15

Is it possible to close browsers, using JavaScript, PHP, or HTML without a prompt?

I was using this answer: How can I close a browser window without receiving the "Do you want to close this window" prompt?

I was using Internet Explorer but it has since been updated to version 10 (soon to be updated to 11) and this exploit does not work.

If there is a solution in a different (but updated) browser, that could work as well.

Community
  • 1
  • 1
jlrosenberg
  • 287
  • 4
  • 14
  • 4
    Could you elaborate on why you want to do this? As the current answer says, in general, a page that does this is incredibly rude, and I'm very happy that browsers don't allow it. But perhaps you have a good reason, and depending on that reason, another approach may work very well for you. –  Jul 18 '14 at 07:16
  • @hvd: Ever seen those "Close Window" buttons? It would be kinda ridiculous for them to ask you to confirm. – user541686 Jul 20 '14 at 09:28
  • @Mehrdad I've only seen them in pop-up windows where the browsers already don't ask for confirmation. If that were what the OP wanted to use it for, we wouldn't be reading this question. –  Jul 20 '14 at 09:50

2 Answers2

12
  • HTML can't do anything programatic. It's just markup.
  • PHP can only do programatic stuff at your server, not the browser, so no solution there either.
  • Finally, you cannot do it in JS either (unless it's a window opened by a script).

Imagine visiting a web site that closed your browser and you lost all of your open tabs.

Shell Bryson
  • 139
  • 1
  • 5
  • Figured I couldn't do it in PHP and HTML, but thought I'd ask for the hell of it. The JS makes sense as well. Are there no other alternatives to close a browser programmatically? – jlrosenberg Jul 17 '14 at 15:39
  • Or even just close the tab - which invalidates your last point? – jlrosenberg Jul 17 '14 at 15:56
  • 2
    "Can't do it in JS"? That's not right - there are browsers where it can be done. If there's a spec that says it can't be done, please quote it. – Marcin Jul 17 '14 at 17:48
  • I believe you can close a tab with js. You can also write a js extension to many browsers which will allow this. – Hogan Jul 17 '14 at 17:52
  • 3
    @Marcin: The spec you requested: https://developer.mozilla.org/en-US/docs/Web/API/Window.close And the MS one: http://msdn.microsoft.com/en-us/library/ie/ms536367(v=vs.85).aspx – NotMe Jul 18 '14 at 16:47
0

There are different hacks for different browsers. This code is based on the one here with a small adjustment to the last else block to work on IE 11. I suggest you test it on other IE versions and see if it needs more tweaking.

function closeWindow() {
    var Browser = navigator.appName;
    var indexB = Browser.indexOf('Explorer');

    if (indexB > 0) {
        var indexV = navigator.userAgent.indexOf('MSIE') + 5;
        var Version = navigator.userAgent.substring(indexV, indexV + 1);

        if (Version >= 7) {
            window.open('', '_self', '');
            window.close();
        }
        else if (Version == 6) {
            window.opener = null;
            window.close();
        }
        else {
            window.opener = '';
            window.close();
        }

    }
    else {
        window.open('', '_self', '');
        window.close();
    }
}
gafi
  • 12,113
  • 2
  • 30
  • 32