How close current window tab in all browsers using javascript

Some 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()” />
For example 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.
For read more please follow this link
Closing Tab/Window using window.close

22 Replies to “How close current window tab in all browsers using javascript”

  1. I tried to send a reply: “Super!” but you have put up the barriers by insisting we fill in other boxes, email and name, when many like me never do that because we can’t be bothered. However as you see I have done it for you because you are the only site I found that has a solution AND your explanation was so clear and works exactly as you stated. Only slight misgiving is that the double quotes you use do not copy/paste into Notepad++ so I had to manually replace them (about ten seconds work with Notepad++!). Thank you.

      1. sir my one query web browser per koi new tab open kr or old tab close ho in javascript m koi command ho to batana sir plz help me sir

  2. An interesting discussion is definitely worth comment.
    I do think that you should publish more about this subject matter, it may not be a taboo subject but generally people don’t talk about such issues.
    To the next! Kind regards!!

  3. Hello Raghunath,
    Your and other solution worked if i have one tab and click on close me button but i when you redirect page to other page let say default.aspx one page and default1.aspx is other page and close button is on first page and other button for redirect to 2nd page and when come back to first page that close me not work what is problem is their any setting to chrome because it work in Mozilla and IE also .
    Thanks

      1. Actually problem is that let say i have three pages one is login.aspx and other is mainpage.aspx and logout.aspx. When i click on logout button on mainpage i am redirect to logout.aspx page and on logout page i have relogin and exit button that exit button is not working in chrome as it is not open in window.open as other says. reply if have need more detail about my problem

  4. hi everyone does anyone have solution for my problem that is i want to disable current tab using javascript plz give solution ASAP
    thanks in advance
    Pradeep

Leave a Reply

Your email address will not be published. Required fields are marked *