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>

51 Responses to How To Close Browser Windows In Windows Internet Explorer 7

  • Catto says:

    Hey Now Paulo,

    Nice little piece of info.

    Thx,

    Catto

  • Frank says:

    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

  • michal says:

    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

  • That’s odd.

  • Victor says:

    Simply, expectacular, thank you very much.

  • Thanks, Victor.

  • Dean says:

    Many thanks, i’ve been searching for hours.

  • Pranab says:

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

    great job u all

  • Ernie says:

    Doesn’t work in IE7 on Vista

  • It’s working for mim on Vista x64 SP1.

    Have you tried Michal’s solution?

  • sid says:

    hey that was great!
    I looked at lot of web pages but
    javascript was the best
    thanks!

  • Ted says:

    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 says:

    My bad. It does work.

  • Chris says:

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

  • Steven says:

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

  • Steven Ray says:

    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.

  • paulo says:

    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.

  • Fabian (Brazil) says:

    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

  • paulo says:

    I’m glad you’re happy.

  • Magic says:

    Thanks,

    It’s a Nice Job Man. !

  • Ethrad says:

    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.

  • paulo says:

    It’s working fine here on IE8.

    What error are you getting?

  • Bob Garringer says:

    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

  • paulo says:

    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.

  • Gilad Balas says:

    Thanks a lot !
    took me two hours to find this post.

  • paulo says:

    Glad to be of help!

  • Glenn Rumfellow says:

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

  • paulo says:

    Thanks, Glenn.

  • Sami says:

    Thanx dude that totally works

  • aru says:

    Woow thanks a tonne buddy.. it works 🙂

  • paulo says:

    Always glad to help, aru.

  • JChan says:

    fabulaso..it helped to solve a issue which was bugging for too much time..thanks Paulo

  • Mitul says:

    Thanx its workin nice…

  • Gary says:

    Thanks so much, this small piece of code saved so much time and bother!!!

  • CJ says:

    THANK YOU!!!!!!!
    you saved my life!

  • Raj Timsina says:

    Try this excellent piece of code for closing IE without confirmation. ENJOY DUDE



    Excellent Self Close IE





  • paulo says:

    Exactly what I’ve shown. Right, Raj?

  • Min says:

    Thanks so much!!! You saved a lot of headaches from me.

  • Jomo says:

    Looked in many places but only this worked to perfection. Thank a gazillion!

  • EL says:

    @ Paulo M.

    We did try this but on IE8 8.0.6001.18702 we are still prompted to allow the page to close the tab 🙁

  • lisa corpus says:

    Close windows

  • paulo says:

    @EL

    I don’t have any IE8 at hand at the moment, but I’ve tested with I9 and what doesn’t work is setting window.close.

    Try this code:

    <!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”>
        function windowClose()
        {
            window.open(“”,”_self”);
            window.close();
        }
        </script>
    </head>
    <body>
    <input type=”button” value=”Close Me!” onclick=”window.close()” />
    <input type=”button” value=”Close Me!” onclick=”windowClose()” />
    </body>
    </html>

    and call windoClose when you need to close the window.

    Let me know how it goes.

  • paulo says:

    @EL,

    I tried with IE 8.0.760.16385 and it was working fine.

    Probably something related to Windows 7 SP1. I’ll have to check it out.

  • Jack E. Meador says:

    I am trying to close IE9 browser. How do I do this?

  • paulo says:

    What sort of problems are you running into, Jack? Have you checked your browser security settings to see if something is causing this?

  • Lou says:

    can you help me please I am trying to close Windows internet explorer browser
    Lou

  • paulo says:

    Lou,

    How are you trying and what problems are you having?

  • GRACE pIETRAMALA says:

    TRYING TO CLOSE BROWSER fter downloading updated java script. it is asking me to close the browser

  • paulo says:

    Can explain better what difficulties you are running into?

  • Bob Tujetsch says:

    I was advised on my computer to close my browser. Why was I told to do this? Is this usually recommended?

  • paulo says:

    @Bob,

    What was the exact message you got?