Close a window or tab in Firefox with JavaScript…
Update… Unfortunately this code no longer works in Firefox 2.x ~ I guess they saw this and fixed it, (for them - broke it for us!).
Recently at work I was asked to make javascript:window.close(); work in FireFox. I searched hard and long across numerous forums and on each and every one the answer was the same - it cannot be done unless the page was opened by a script! Or at least it couldn’t until I was required to make it happen
I’m by no means a JavaScript expert - in fact I hardly know any at all - but I couldn’t believe that this was completely impossible so I came up with the following:
The first step is to fool the browser into thinking that it was opened with a script…
window.open('','_parent','');
This opens a new page, (non-existent), into a target frame/window, (_parent which of course is the window in which the script is executed, so replacing itself), and defines parameters such as window size etc, (in this case none are defined as none are needed). Now that the browser thinks a script opened a page we can quickly close it in the standard way…
window.close();
and there you have it - I told you it was simple! In case you didn’t follow that, here is the complete solution in two easy steps:
1. Copy/paste the following code to the head of your page…
<script language="javascript" type="text/javascript">
function closeWindow() {
window.open('','_parent','');
window.close();
}
</script>
2. Set your link like this:
<a href="javascript:closeWindow();">Close Window</a>
and there you have it - a problem that it seemed, (according to my trying to find a solution before coming up with this one anyway), to have baffled everyone else.
February 10th, 2006 at 12:39 am
p.s. Feel free to use this solution but a mention or a link back to this page would be appreciated
February 14th, 2006 at 5:14 pm
[…] to Lure, I now know that the good folks over at Interwebby discovered a solution to the Firefox windowclosing problem! Rather than steal their thunder, I’ll let you visit Interwebby yourself. It’s really a simple, elegant solution! […]
February 22nd, 2006 at 8:56 am
Yep Sure closes the FF browser window.
Unfortunately it closes the window completely and not just the tab when using tabs on FF.
Hmm
March 2nd, 2006 at 4:25 pm
I can’t seem to make that code work on my Firefox.
I opened a new window using this code:
window.open(”PersonList.aspx”, “FindBarcodeWindow”, “dependent=yes,width=300,height=320″);
I have tried removing the dependent=yes, but still cannot close the new window.
The new window has the closeWindow() function as mentioned in the original post above.
March 2nd, 2006 at 5:45 pm
bluetongue - was it the only tab open or one of several? I’ve tested it with several tabs open and it just closed the one I wanted.
iws - If you opened the window with a script then you should just be able to close it with a standard window.close();
March 2nd, 2006 at 5:48 pm
To anyone having problems getting this to work, it may work better with <a onclick=”closeWindow();”> instead of <a href=”javascript:closeWindow();”>
March 6th, 2006 at 11:46 pm
This method works on windows platform for IE, Firefox and Netscape. But, on Linux and Mac platform, it will open a new window instead of closing it. Anyone having the same problem? Care to share the solution?
March 24th, 2006 at 2:42 pm
I’ll be getting a Mac at work soon for testing so if I come up with a solution for these browsers I’ll let you know
April 5th, 2006 at 4:05 am
Thank you.
April 5th, 2006 at 4:30 pm
Orphelia: I got my Mac at work and checked this on FF and Safari and it worked fine on both.
May be that I got an Intel Mac though, can’t be sure.
Magiadiaraia: Your welcome
April 25th, 2006 at 8:36 am
Works beautifully with my application!
I have been looking for solution for months. Many many many thanks.
July 6th, 2006 at 4:35 pm
Thanks so much. I get alot of hassle from the guys at work about Firefox issues, but they all come down to the fact that IE never seems to holds to any standards. Thanks again for letting me rub yet another IE issue in their face
August 10th, 2006 at 1:53 pm
Popups CAN be closed in Firefox…
After seeing “No, it’s impossible” virtually everywhere, I finally found a post with a hilariously short and simple (hence, brilliant) solution to yes, closing pop-up windows in Firefox. Very handy.
……
September 28th, 2006 at 8:21 am
Thanks for the solution to the firefox. But it was successful till certain extent.
When there is only single tab in the FF, then whole window gets closed that to expire the session.
But whenever there are multiple tabs & I am using this script, then it closes that perticular tab. which is not closing the session.
i.e. if I am opening any other tab in the same FF window, I get the old session - without login again. Which is wrong.
Please help me to resolve this issue.
Thanks in advance.
Ganesh
September 28th, 2006 at 10:54 am
Nice solution. Thanks very much. You can compress this down to a single line as follows :
javascript:window.open(’javascript:window.close();’,'_self’,'’);