94

How can I close a browser window without receiving the Do you want to close this window prompt?

The prompt occurs when I use the window.close(); function.

William Perron
  • 485
  • 7
  • 16
Derek
  • 16,181
  • 3
  • 27
  • 36

18 Answers18

91
window.open('', '_self', ''); window.close();

This works for me.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Arabam
  • 1,009
  • 7
  • 4
67

Scripts are not allowed to close a window that a user opened. This is considered a security risk. Though it isn't in any standard, all browser vendors follow this (Mozilla docs). If this happens in some browsers, it's a security bug that (ideally) gets patched very quickly.

None of the hacks in the answers on this question work any longer, and if someone would come up with another dirty hack, eventually it will stop working as well.

I suggest you don't waste energy fighting this and embrace the method that the browser so helpfully gives you — ask the user before you seemingly crash their page.

rvighne
  • 20,755
  • 11
  • 51
  • 73
  • 5
    Or, even simpler: Don't close the window at all. If you're done, send the user back to your home page, or to some other sort of central location. –  Jul 20 '14 at 20:11
  • 3
    @duskwuff, We're running automated tests against these browsers. They need to *close*. – ouflak Dec 01 '17 at 08:42
  • 2
    @ouflak Surely your testing framework has some way to manipulate windows. –  Dec 01 '17 at 08:44
  • 1
    @duskwuff What about third-party libraries that leave windows laying around? You surely can't be suggesting that I just leave that mess for the user or redirect them to a (duplicate) view of the home page. – Abandoned Cart Nov 28 '18 at 20:07
  • @AbandonedCart If the library opened the window *itself* (i.e, the window wasn’t opened by the user), it should be able to close that window itself. If it won’t do that, consider using another library I guess? –  Nov 28 '18 at 20:17
  • @duskwuff And if it's the "official" version or the only one that exists? – Abandoned Cart Nov 28 '18 at 20:24
  • 1
    Could you explain also in your answer as a footnote, why it's security risk. – T.Todua Jan 28 '19 at 11:14
  • Unless its IE11, they aint gonna update that again - im adding my monkey hacks - skurt skurt – Coty Embry Oct 01 '20 at 19:47
58

My friend... there is a way but "hack" does not begin to describe it. You have to basically exploit a bug in IE 6 & 7.

Works every time!

Instead of calling window.close(), redirect to another page.

Opening Page:

alert("No whammies!");
window.open("closer.htm", '_self');

Redirect to another page. This fools IE into letting you close the browser on this page.

Closing Page:

<script type="text/javascript">
    window.close();
</script>

Awesome huh?!

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Nick
  • 19,198
  • 51
  • 185
  • 312
  • 1
    I know it's already been 2 years. But it's amazing..Works on IE 8 and firefox 15 – Susie Sep 07 '12 at 22:44
  • 9
    Looks like Microsoft has fixed this bug in IE10. Anybody know how we can still achieve this in IE10? – aram063 Jun 23 '13 at 23:46
  • Both self.close() and window.close() do not work for IE on my side. However, Chrome, FF and Safari are fine – NoName Sep 18 '15 at 06:57
33

Here is Javascript function which I use to close browser without Prompt or Warning, it can also be called from Flash. It should be in html file.

    function closeWindows() {
         var browserName = navigator.appName;
         var browserVer = parseInt(navigator.appVersion);
         //alert(browserName + " : "+browserVer);

         //document.getElementById("flashContent").innerHTML = "<br>&nbsp;<font face='Arial' color='blue' size='2'><b> You have been logged out of the Game. Please Close Your Browser Window.</b></font>";

         if(browserName == "Microsoft Internet Explorer"){
             var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;  
             if (ie7)
             {  
               //This method is required to close a window without any prompt for IE7 & greater versions.
               window.open('','_parent','');
               window.close();
             }
            else
             {
               //This method is required to close a window without any prompt for IE6
               this.focus();
               self.opener = this;
               self.close();
             }
        }else{  
            //For NON-IE Browsers except Firefox which doesnt support Auto Close
            try{
                this.focus();
                self.opener = this;
                self.close();
            }
            catch(e){

            }

            try{
                window.open('','_self','');
                window.close();
            }
            catch(e){

            }
        }
    }
Kuldip D Gandhi
  • 385
  • 4
  • 7
  • On IE 10.0.9200, opening the window using `showModalDialog`. On `closeWindows`, It opens a new window (the same page) while the previous one is still open. – bjan Mar 09 '16 at 05:45
  • Didn't work on Firefox, IE/Edge displayed a confirmation prompt - it worked on Chrome though. – Dymas May 03 '19 at 13:49
12

This works in Chrome 26, Internet Explorer 9 and Safari 5.1.7 (without the use of a helper page, ala Nick's answer):

<script type="text/javascript">
    window.open('javascript:window.open("", "_self", "");window.close();', '_self');
</script>

The nested window.open is to make IE not display the Do you want to close this window prompt.

Unfortunately it is impossible to get Firefox to close the window.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
11

In the body tag:

<body onload="window.open('', '_self', '');">

To close the window:

<a href="javascript:window.close();">

Tested on Safari 4.0.5, FF for Mac 3.6, IE 8.0, and FF for Windows 3.5

JimB
  • 319
  • 2
  • 5
  • That's functionally the same thing Nick did. – naugtur Dec 13 '10 at 20:44
  • Yes. If I'm reading Nick's post correctly he suggests a redirect before calling window.close. What I posted is if you don't want to use a redirect. If I'm misreading Nick's post apologies. – JimB Dec 16 '10 at 20:08
7

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

Sorry a bit late here, but i found the solution, at least for my case. Tested on Safari 11.0.3 and Google Chrome 64.0.3282.167

Mada Aryakusumah
  • 611
  • 11
  • 16
  • Works on Firefox version 88, too, if the configuration option `dom:allow_scripts_to_close_windows` is set to true (it's false by default). – Suncat2000 Mar 29 '21 at 17:15
  • How is this answer any different than the one provided as a comment in the original question? – oyalhi Jun 27 '22 at 23:36
7

For security reasons, a window can only be closed in JavaScript if it was opened by JavaScript. In order to close the window, you must open a new window with _self as the target, which will overwrite your current window, and then close that one (which you can do since it was opened via JavaScript).

window.open('', '_self', '');
window.close();
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
jbabey
  • 45,965
  • 12
  • 71
  • 94
  • this worked for me with Silverlight: HtmlPage.Window.Eval("window.open('', '_self', ''); window.close();"); – Mike Fuchs Oct 10 '11 at 13:31
6

Because of the security enhancements in IE, you can't close a window unless it is opened by a script. So the way around this will be to let the browser think that this page is opened using a script, and then to close the window. Below is the implementation.

Try this, it works like a charm!
javascript close current window without prompt IE

<script type="text/javascript">
function closeWP() {
 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.close();
 }
}
</script>

javascript close current window without prompt IE

ragingasiancoder
  • 616
  • 6
  • 17
Vivek
  • 5,025
  • 2
  • 18
  • 9
6

From here:

<a href="javascript:window.opener='x';window.close();">Close</a>

You need to set window.opener to something, otherwise it complains.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Harley Holcombe
  • 175,848
  • 15
  • 70
  • 63
2

Create a JavaScript function

<script type="text/javascript">
    function closeme() {
        window.open('', '_self', '');
        window.close();
    }
</script>

Now write this code and call the above JavaScript function

<a href="Help.aspx" target="_blank" onclick="closeme();">Help</a>

Or simply:

<a href="" onclick="closeme();">close</a>
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Niloofar
  • 723
  • 8
  • 9
2

This will work :

<script type="text/javascript">
function closeWindowNoPrompt()
{
window.open('', '_parent', '');
window.close();
}
</script>
2
window.opener=window;
window.close();
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
1

In my situation the following code was embedded into a php file.

var PreventExitPop = true;
function ExitPop() {
  if (PreventExitPop != false) {
    return "Hold your horses! \n\nTake the time to reserve your place.Registrations might become paid or closed completely to newcomers!"
  }
}
window.onbeforeunload = ExitPop;

So I opened the console and write the following

PreventExitPop = false

This solved the problem. So, find out the JavaScript code and find the variable(s) and assign them to an appropriate "value" which in my case was "false"

nurmurat
  • 151
  • 1
  • 11
0

Place the following code in the ASPX.

<script language=javascript>
function CloseWindow() 
{
    window.open('', '_self', '');
    window.close();
}
</script>

Place the following code in the code behind button click event.

string myclosescript = "<script language='javascript' type='text/javascript'>CloseWindow();</script>";

Page.ClientScript.RegisterStartupScript(GetType(), "myclosescript", myclosescript);

If you dont have any processing before close then you can directly put the following code in the ASPX itself in the button click tag.

OnClientClick="CloseWindow();"

Hope this helps.

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
  • 1
    The questions asks for a simple javascript solution, avoid ASPX. – Zeeshan Dec 05 '15 at 10:09
  • @Zeeshan: Any developer worth their salt can see that the JavaScript at the top of Kamleshkumar Gujarathi's post can be applied to their own requirement. A touch harsh if you downvoted his answer. – Paul Dec 30 '15 at 11:57
0

The browser is complaining because you're using JavaScript to close a window that wasn't opened with JavaScript, i.e. window.open('foo.html');.

Billy Jo
  • 1,326
  • 19
  • 32
-2

The best solution I have found is:

this.focus();
self.opener=this;
self.close();
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Derek
  • 16,181
  • 3
  • 27
  • 36
-3

I am going to post this because this is what I am currently using for my site and it works in both Google Chrome and IE 10 without receiving any popup messages:

<html>
    <head>
    </head>
    <body onload="window.close();">
    </body>
</html>

I have a function on my site that I want to run to save an on/off variable to session without directly going to a new page so I just open a tiny popup webpage. That webpage then closes itself immediately with the onload="window.close();" function.

Logan Hasbrouck
  • 416
  • 1
  • 7
  • 21
  • Yes : but like http://stackoverflow.com/a/24854246/2239406 information : you open the windo in javascript, then can close with javascript. – Denis Chenu Oct 23 '15 at 12:59
  • well, if it is not supposed to work, and if this has been 'patched', it still works on all browsers on my pc without fail. I was simply providing a solution that was currently working for me. – Logan Hasbrouck Oct 24 '15 at 03:44
  • The difference is : you open the window in JS. If the window si not open in JS : don't work. – Denis Chenu Oct 27 '15 at 07:36
  • No, I am not using JS to open the window; rather I am using an `a` with `target=_blank` to open a tiny popup window at a specific url, then finally I use JS to close the window. It's purpose is that the specific url saves a value to the session to disable background images, although here soon I will be deleting it in favor of ajax once I figure out how to do ajax. (still learning the ropes persay) – Logan Hasbrouck Oct 27 '15 at 13:41