Friday, 11 January 2013

Close Current Browser Tab Using Javascript

Close Current Browser Tab Using Javascript

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 ="","_self"); /* url = "" or "about:blank"; target="_self" */



< /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.



  1. Thanks for this. Just what I was looking for!

  2. Very nice!
    I think, though, there's no need for variable "win":"", "_self").close();

  3. Just perfect! Thank u! :-)


Thank You for Your Comments. We will get back to you soon.

back to top