ISSUE :
 
Profile photo
Submitted by David Wu
on May 23rd, 2011 at 22:17:13 EST

window.close() does not work in Firefox and Chrome

I am trying to use following:
<a href="#" onclick="javascript:window.close();return false;">Close</a>

But this does not work in Firefox and Chrome. Is there anyway to make window.close() work in firefox and chrome?


Profile photo
Replied by Misuk Wang
on May 23rd, 2011 at 22:32:21 EST
Use var win = window.open('', '_self'); win.close();
<a href="#" onclick="javascript:var win = window.open('', '_self');win.close();return false;">Close</a>

This will close the current window in Chrome and IE. However, Firefox is very strict on using window.close().
Mozilla documentation specifically says, window.close() will work only if the window is opened using window.open().
https://developer.mozilla.org/en/window.close

So, window.close() will not be possible in Firefox if the window is not opened by using window.open().
Accepted Answer
Accepted
 Answer

Profile photo
Replied by David Wu
on May 23rd, 2011 at 22:35:01 EST
This is not what I was hoping for. But its at least good to know that in Firefox window.close() will not work at all if the window is not opened by using window.open().

Profile photo
Replied by Tiago R
on October 7th, 2011 at 04:08:26 EST
parent.close();

example:
<a href="javascript:parent.close();" target="_top">Close</a>

Profile photo
Replied by Marsowl ALsahely
on April 18th, 2014 at 22:43:23 EST
How I add this code under asp Button?
<a href="javascript:parent.close();" target="_top">Close</a>

Profile photo
Replied by SBajra
on April 19th, 2014 at 16:18:50 EST
You can add javascript to ASP.NET button using OnClientClick even.


<asp:Button Text="Close" runat="server" OnClientClick="javascript:var win = window.open('', '_self');win.close();return false;" />