How can I close a browser window without receiving the "Do you want to close this window" prompt?
window.open('', '_self', ''); window.close();
This works for me.
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.
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?!
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){
}
}
}