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

JavascriptBrowser

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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

This works for me.

Solution 2 - Javascript

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.

Solution 3 - Javascript

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?!

Solution 4 - Javascript

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){

			}
		}
	}

Solution 5 - Javascript

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.

Solution 6 - Javascript

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

Solution 7 - Javascript

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();

Solution 8 - Javascript

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

Solution 9 - Javascript

From here:

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

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

Solution 10 - Javascript

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

Solution 11 - Javascript

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

Solution 12 - Javascript

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>

Solution 13 - Javascript

This will work :

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

Solution 14 - Javascript

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"

Solution 15 - Javascript

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');.

Solution 16 - Javascript

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.

Solution 17 - Javascript

The best solution I have found is:

this.focus();
self.opener=this;
self.close();

Solution 18 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDerekView Question on Stackoverflow
Solution 1 - JavascriptArabamView Answer on Stackoverflow
Solution 2 - JavascriptrvighneView Answer on Stackoverflow
Solution 3 - JavascriptNickView Answer on Stackoverflow
Solution 4 - JavascriptKuldip D GandhiView Answer on Stackoverflow
Solution 5 - JavascriptDanny BeckettView Answer on Stackoverflow
Solution 6 - JavascriptJimBView Answer on Stackoverflow
Solution 7 - JavascriptjbabeyView Answer on Stackoverflow
Solution 8 - JavascriptMada AryakusumahView Answer on Stackoverflow
Solution 9 - JavascriptHarley HolcombeView Answer on Stackoverflow
Solution 10 - JavascriptVivekView Answer on Stackoverflow
Solution 11 - JavascriptMarkView Answer on Stackoverflow
Solution 12 - JavascriptNiloofarView Answer on Stackoverflow
Solution 13 - Javascriptuser5585864View Answer on Stackoverflow
Solution 14 - JavascriptnurmuratView Answer on Stackoverflow
Solution 15 - JavascriptBilly JoView Answer on Stackoverflow
Solution 16 - JavascriptKamleshkumar GujarathiView Answer on Stackoverflow
Solution 17 - JavascriptDerekView Answer on Stackoverflow
Solution 18 - JavascriptLogan HasbrouckView Answer on Stackoverflow