views:

6261

answers:

4

Suppose I want to have a link or a button that when user click it, the browser will close without any confirmation dialog box.

It will need to work on Internet Explorer 6, 7, 8 and Firefox.

+5  A: 

Duplicate: how-can-i-close-a-browser-window-without-receiving-the-do-you-want-to-close-this

Another great SO answer on this topic:

Duplicate: closing-browser-after-failing-to-login

And just another duplicate:

how-to-close-browser-in-a-given-time

Ghommey
Thanks for the reply but the code in that page doesn't work in Internet Explorer 8.
natch3z
A: 

That would be a major security breach. You will never be able to do that.

You can, however, close child windows the parent has opened. Say you opened a popup with a parent window, that same parent window can have a button to close the child. Never the browser.

Frankie
I need to close the main browser because of security concern. We have to make sure that after user logs off the system, the browser is close automatically.
natch3z
Can I assume that you are using http_auth based authentication? If security is a big issue, and if you must be sure the session expires after some time, I believe you should take a look into other ways of keeping sessions. You are trying to hack your way into security! :) Do it the other way around. Secure your way into hacks! ...
Frankie
Any time your attempt at implementing security is thwarted by an existing security feature in the browser, you might want to reevaluate your technique.
Chris Farmer
A: 

I think part of the issue here is that you are unclear about your goal in the question. From your comment on Frankie's answer, it seems that you don't really need to close the window at all, you simply need to secure the content that was available in the window.

Here is what I recommend:

  1. Kill the logged-in-session based on a (server-side) timeout. When the timeout is up, a reload of the page should only redirect to the login page.
  2. On the client side, periodically check for timeout conditions and redirect to a login page, or some other doesn't-need-to-be-secured page.

Frankly, it's usually better to keep a user on your site until the moment that -they- want to close the tab or otherwise browse away anyway. Just redirect them to a landing page that doesn't need to be secure instead of trying to force closing of the tab.

Tchalvak
+1  A: 

I have done some research and found out that it is not possible to close window/tab in Firefox if that window/tab isn't open through javascript or if the tab has history pages > 1 (i.e. Back button clickable because you browse through webpages).

Way to do in Firefox : Delete the history first. Then you can close window without confirmation box. I haven't tried this solution. I happen to read through multiple reliable pages that describe how to do this.

Solution for Internet Explorer 6, 7, 8.

With a little help from browser detect function : http://www.quirksmode.org/js/detect.html , here is how to close window without comfirmation box for multiple IE versions.

if ((userBrowser.browser == "Explorer" && (userBrowser.version == "8" || userBrowser.version == "7"))) {
        window.open('', '_self', '');
        window.close();
    } else if ((userBrowser.browser == "Explorer" && userBrowser.version == "6")) {
        window.opener = null;
        window.close();
    } else {
        window.opener = '';
        window.close(); // attempt to close window first, show user warning message if fails
        alert("To avoid data corruption/loss. Please close this window immedietly.");
    }

I know that some of you might think that using browser detect isn't a good idea. Also, many believe that forcing users to close window is a bad thing. I agree to those idea. But, I just need it because of software requirement we are told to do.

Hope this helps.

natch3z
Works with IE6 and IE7 but not in Firefox 3.5
Sauron
Yes. The code above does not work with Firefox so I add the last sectionalert("To avoid data corruption/loss. Please close this window immedietly."); to alert the user.If you really need to close Firefox, you need to write a code to delete history so that the page the is about to close is the first page of the tab (mimicing that it is the first page that user opens) then you will be able to close the page.
natch3z