Javascript trick: closing a Firefox tab

Find out that the plain ol' window.close() doesn't actually close the window if the window is a tab in Firefox?  Basically, it doesn't consider the tab to be opened by script... so you need to trick the browser into thinking that hte page was opened by script, then close it.  Interwebby.com came up with this nifty workaround:

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

 

posted on Tuesday, September 26, 2006 11:36 AM by snyholm

Comments

# re: Javascript trick: closing a Firefox tab

This script no longer works in Firefox 2.0
Tuesday, October 24, 2006 9:46 PM by David

# re: Javascript trick: closing a Firefox tab

@David: There is a possibility to get it to work with Firefox 2.0: Open about:config and set dom.allow_scripts_to_close_windows to true. Then it should work.

I've implemented a function to do close a window/tab in IE6. The scripts also works with, FF1.5, FF2.0, IE6, IE7.
Here is the code:

<SCRIPT LANGUAGE="JavaScript">

function closeWindow()
{
//Firefox
if (navigator.appName == 'Netscape')
{
window.open('', '_self', '');
window.close();
}
else //Internet Explorer
setTimeout("WB.ExecWB(45,2)",0);
}

</SCRIPT>

<OBJECT ID="WB" WIDTH="0" HEIGHT="0" CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2">
</OBJECT>

<body onload="self.focus(); closeWindow()"></body>

Saturday, November 18, 2006 12:36 PM by Chris Rock

# re: Javascript trick: closing a Firefox tab

Even the other one too doesnt work with firefox 2.0
Thursday, December 14, 2006 1:16 AM by Balamurugan