7/05/2006

Closing Tab/Window in Mozilla using window.close

Category: workaround

Mozilla browsers will not allow you to close the window using window.close() unless the script opened the window. This is a little annoying sometimes. But there is a workaround to resolve this issue. If you observe the error message that is thrown by Mozilla Firefox, "Scripts may not close windows that were not opened by the script", it clearly says that if the script didn't open the window, you can't close that. But we open a blank page in the same window using "_self" as the target window and close the same window. In that way, the script opens the window (which is a blank one) and closes the window too.

So, to put this in the code,

< script >
function closeMe()
{
var win = window.open("","_self"); /* url = "" or "about:blank"; target="_self" */
win.close();
}
< /script >
< input type="button" name="CloseMe" value="Close Me" onclick="closeMe()" />

In mozilla, if there are more than one tabs open, then only the tab where this script is being executed, will be closed, not the whole window. But if there is only one tab open where this script is executing then the window will be closed.

Tested this script in Mozilla Firefox 1.5.0.4 and IE 6, XP SP2, Mozilla 1.7.12. I didn't test in any other browser.

13 comments:

sacakukac said...

Not working in Firefox 2.0.0.4...
:(

olly said...

window.close() no


nadda nadda nadda nadda nadda ahh so annoying seems to work fine if u have opened up a popup window - but not coming from the parent window doooohoohohoohoh oh yeah got v2.0.0.0.0.0.0.0.0.0..0.0.0.7 mozilla

Anonymous said...

Hi pavan,

its not working, i have tried this in mozilla(with more than 1 tab).

put only those that u have tried.


3nadh

Pavan Keely said...

Mr 3nadh,

Please read my whole article before posting your comments. I have clearly specified "Tested this script in Mozilla Firefox 1.5.0.4 and IE 6, XP SP2, Mozilla 1.7.12. I didn't test in any other browser.". That means I tested and I put this code here. I do not put the code unless I test it personally. If it's not working in another version of Mozilla, may be they have restricted this.

Anonymous said...

still it is not working. plz give a correct coding plz

mallikarjuna

Ashwin said...

var win=window.open("about:blank","_self");
win.close();


This code works even in the Mozilla 3.0.1

Vipin Gupta said...

Hi Ashwin,

var win=window.open("about:blank","_self");
win.close();

is not closing the brower tab but what it is doing is just navigating my tab to the mentioned URL in script (about:blank).

Can you please suggest how can I close the mozila browser window/tab.

Reelix said...

Does not work in Firefox 3.0.6.

Ross duPlessis PGA Professional said...

Thanks... been all over the web looking just for this....needed to close a window that was opened by a php form and straight java script could not get it done....

Anonymous said...

Go to FF about:config, change the 'dom.allow_scripts_to_close_windows' to true. It will then close the tab. By default this parameter is set to false.

Uzair Tahir said...

var win=window.open("about:blank","_self");
win.close();


This code works in Fire Fox.

Anonymous said...

Trick did well for me.

Thanks Pavan keep posting such a freaking work.

:thumbsup:

Anonymous said...

I like this very much. Any idea how to make it work on an IPad? There it 'goes' to the url (leaving a plain white screen) but never performs the close.