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).
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> <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){
}
}
}
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.
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.
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:
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.
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"