How To Close Browser Windows In Windows Internet Explorer 7

When a web page uses scripting to close a browser window that was opened by the user and not opened by some action on another page, Internet Explorer pops up a question to the user warning that "The webpage you are viewing is trying to close the tab." (in this case, Internet Explorer 7) and asking the user for permission to close the tab.

Before Internet Explorer 7, all that was needed to do was setting the window.opener property to a non null value:

window.opener = self;
window.close();

Unfortunately, Internet Explorer 7 isn't fooled by this. Internet Explorer 7 knows if the window was opened by the user or not, regardless the value of the window.opener property.

Fortunately, Internet Explorer can still be fooled:

window.open("","_self");
window.close();

Going one step further, if you want all your calls to the window.close method to work this way, you can change the method implementation like in the following example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <script type="text/javascript">
    // Save a reference to the original method.
    var windowClose = window.close;

    // Re-implement window.open
    window.close = function ()
    {
        window.open("","_self");
        windowClose();
    }
    </script>
</head>
<body>
<input type="button" value="Close Me!" onclick="window.close()" />
<input type="button" value="Close Me!" onclick="windowClose()" />
</body>
</html>
Published Friday, November 02, 2007 2:38 AM by Paulo Morgado

Comments

Friday, November 02, 2007 3:54 AM by DotNetKicks.com

# How To Close Browser Windows In Windows Internet Explorer 7

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Friday, November 02, 2007 11:32 AM by Catto

# re: How To Close Browser Windows In Windows Internet Explorer 7

Hey Now Paulo,

Nice little piece of info.

Thx,

Catto

Wednesday, December 26, 2007 3:02 PM by Frank

# re: How To Close Browser Windows In Windows Internet Explorer 7

This is nice. I've searched umpteen references and this is the first fix that I've found that actually works and is simple to implement.

thanks

Tuesday, January 15, 2008 4:21 PM by TrackBack

# http://dotnetkicks.com/aspnet/how_to_close_browser_windows_in_windows_internet_explorer_7

Wednesday, February 06, 2008 5:40 AM by michal

# re: How To Close Browser Windows In Windows Internet Explorer 7

Cheers !

I've been looking for this for a long time.

Actually i had to change the code like this:

       var windowClose = window.close;
       // Re-implement window.open
       window.close = function ()
       {
           window.opener = '';
           window.open("","_self");
           windowClose();
       }
window.opener = '';
window.close();

and now it's wrking perfectly for both IE6 and IE7

Thanks again

Wednesday, February 06, 2008 6:08 AM by Paulo Morgado

# re: How To Close Browser Windows In Windows Internet Explorer 7

That's odd.

Wednesday, February 27, 2008 9:33 AM by Victor

# re: How To Close Browser Windows In Windows Internet Explorer 7

Simply, expectacular, thank you very much.

Wednesday, February 27, 2008 4:55 PM by Paulo Morgado

# re: How To Close Browser Windows In Windows Internet Explorer 7

Thanks, Victor.

Friday, April 04, 2008 4:38 PM by Dean

# re: How To Close Browser Windows In Windows Internet Explorer 7

Many thanks, i've been searching for hours.

Wednesday, April 16, 2008 4:13 AM by Pranab

# re: How To Close Browser Windows In Windows Internet Explorer 7

Really nice...........it is something like magic...........

great job u all

Wednesday, May 28, 2008 9:51 AM by Ernie

# re: How To Close Browser Windows In Windows Internet Explorer 7

Doesn't work in IE7 on Vista

Wednesday, May 28, 2008 6:34 PM by Paulo Morgado

# re: How To Close Browser Windows In Windows Internet Explorer 7

It's working for mim on Vista x64 SP1.

Have you tried Michal's solution?

Monday, July 28, 2008 6:50 PM by sid

# re: How To Close Browser Windows In Windows Internet Explorer 7

hey that was great!

I looked at lot of web pages but

javascript was the best

thanks!

Friday, August 22, 2008 12:25 PM by Ted

# re: How To Close Browser Windows In Windows Internet Explorer 7

Both the solution and the modification did not work on IE7 for me on an ASP.NET page. Is there something other than the JavaScript I need to know?

Friday, August 22, 2008 12:37 PM by Ted

# re: How To Close Browser Windows In Windows Internet Explorer 7

My bad. It does work.

Monday, August 25, 2008 2:41 PM by Chris

# re: How To Close Browser Windows In Windows Internet Explorer 7

Simply fantastic, I've been scouring the net for this. Thank-you so much

Leave a Comment

(required) 
(required) 
(optional)
(required)