Paulo Morgado

.NET Development & Architecture

This Blog

Syndication

Search

Sponsored By

Tags

News

Unit Test Today! Get Typemock Isolator!

Projects

Books

 

Visitors

Visitor Locations

Community

Email Notifications

Archives

Profile

Disclaimer

The opinions and viewpoints expressed in this site are mine and do not necessarily reflect those of Microsoft, my employer or any community that I belong to. Any code or opinions are offered as is. Products or services mentioned are purchased by me, made available to me by my employer or the manufacturer/vendor which doesn't influence my opinion in any way.

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 Fri, Nov 2 2007 2:38 by Paulo Morgado

Comments

# How To Close Browser Windows In Windows Internet Explorer 7@ Friday, November 02, 2007 3:54 AM

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

DotNetKicks.com

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Friday, November 02, 2007 11:32 AM

Hey Now Paulo,

Nice little piece of info.

Thx,

Catto

Catto

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Wednesday, December 26, 2007 3:02 PM

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

Frank

# http://dotnetkicks.com/aspnet/how_to_close_browser_windows_in_windows_internet_explorer_7@ Tuesday, January 15, 2008 4:21 PM

TrackBack

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Wednesday, February 06, 2008 5:40 AM

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

michal

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Wednesday, February 06, 2008 6:08 AM

That's odd.

Paulo Morgado

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Wednesday, February 27, 2008 9:33 AM

Simply, expectacular, thank you very much.

Victor

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Wednesday, February 27, 2008 4:55 PM

Thanks, Victor.

Paulo Morgado

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Friday, April 04, 2008 4:38 PM

Many thanks, i've been searching for hours.

Dean

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Wednesday, April 16, 2008 4:13 AM

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

great job u all

Pranab

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Wednesday, May 28, 2008 9:51 AM

Doesn't work in IE7 on Vista

Ernie

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Wednesday, May 28, 2008 6:34 PM

It's working for mim on Vista x64 SP1.

Have you tried Michal's solution?

Paulo Morgado

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Monday, July 28, 2008 6:50 PM

hey that was great!

I looked at lot of web pages but

javascript was the best

thanks!

sid

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Friday, August 22, 2008 12:25 PM

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?

Ted

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Friday, August 22, 2008 12:37 PM

My bad. It does work.

Ted

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Monday, August 25, 2008 2:41 PM

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

Chris

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Wednesday, September 10, 2008 7:06 PM

Wonderful for Explorer, but doesn't work at all in Firefox.

Steven

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Wednesday, September 10, 2008 8:03 PM

I learned that this added script only works in the below listed "failed" browsers if the document was initially opened by javascript, not _self or hyperlinked.

I tried this script and it works in IE6, IE7, Netscape 7.6, Firefox 1 and Firefox 1.5, but not in Netscape 8.0, Firefox 2 or Firefox 3.

The above script is merely exploiting a loophole in Explorer, however this loophole has been fixed in the above mentioned browsers in which it does not work. I suppose Internet Explorer will soon follow suite and close the loophole as well, rendering this workaround obsolete? Wish there was an effective solution that covered all the browsers.

Steven Ray

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Thursday, September 11, 2008 2:50 AM

As a user I really hate sites that open windows just because and sites that close windows I opened.

I haven't tried it on IE8, but I'll try and bug it if this script works.

Paulo Morgado

# THANK YOU!!!!!@ Wednesday, September 17, 2008 11:42 AM

thanx!!, thanx!!, thanx!! man!

I hope you use this way of explaining things to solve others computing problems

its the only tip that worked for me with IE 7 tabs

thank you again   God Bless you

Fabian (Brazil)

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Wednesday, September 17, 2008 5:11 PM

I'm glad you're happy.

Paulo Morgado

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Monday, December 08, 2008 3:38 AM

Thanks,

It's a Nice Job Man. !

Magic

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Wednesday, December 17, 2008 4:42 PM

First button asks the same question, whether I'd like to close the browser or not, and the other button receives an error on page message.

Ethrad

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Wednesday, December 17, 2008 5:26 PM

It's working fine here on IE8.

What error are you getting?

Paulo Morgado

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Sunday, December 28, 2008 9:49 PM

Please email an answer to me at bobgarringer@yahoo.com, if you can help.

I'm not a webmaster. I've copied and pasted some html to accomplish some things on a website. Otherwise, I'm pretty well lost when it comes to the kinds of fixes you deal with.

How do I get to the text to do what you suggest above?

I've downloaded IE 8, because a tech from Microsoft indicated that, in version 8, they intended to solve the "webpage-is-trying-to-close-the-tab" issue. The problem persists, however, with a little more delay than in 7.

Help if you can. And also explain to me how you guys can solve the problem with a little effort and Microsoft cannot.

Bob

Bob Garringer

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Monday, December 29, 2008 3:15 AM

Does the sample page not work with you?

I think that Micorsoft's intention is to prevent these kind of hacks from being possible.

And I agree. As a user, I don't like web sites that open windows on my desktop and I also don't like web sites that try to close windows that I excplicitly opened.

Paulo Morgado

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Thursday, April 23, 2009 3:02 AM

Thanks a lot !

took me two hours to find this post.

Gilad Balas

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Thursday, April 23, 2009 3:18 AM

Glad to be of help!

Paulo Morgado

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Friday, November 20, 2009 12:04 PM

Thank you!  I wish your page was ranked higher in Google!  I'll try to get people to link to it.

Glenn Rumfellow

# re: How To Close Browser Windows In Windows Internet Explorer 7@ Saturday, November 21, 2009 11:36 AM

Thanks, Glenn.

Paulo Morgado

Leave a Comment

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