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.
17 comments:
Not working in Firefox 2.0.0.4...
:(
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
Hi pavan,
its not working, i have tried this in mozilla(with more than 1 tab).
put only those that u have tried.
3nadh
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.
still it is not working. plz give a correct coding plz
mallikarjuna
var win=window.open("about:blank","_self");
win.close();
This code works even in the Mozilla 3.0.1
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.
Does not work in Firefox 3.0.6.
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....
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.
var win=window.open("about:blank","_self");
win.close();
This code works in Fire Fox.
Trick did well for me.
Thanks Pavan keep posting such a freaking work.
:thumbsup:
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.
But this is not working on latest mozilla 32.0 , can you have any work around
no way you can do on recent firefox without privilege
Yes nice but this is not working in mozila firefox.Please give me the solutions for this.
it is not working in Chrome Version 74.0.3729.169 (Official Build) (64-bit)
Post a Comment