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

Advertisements

Sr. Software Engineer

Tagged with: , , , ,
Posted in PHP
21 comments on “How close current window tab in all browsers using javascript
  1. Lavanya says:

    Thankyou

  2. richard crane says:

    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.

  3. Doesn’t work on FF 22.0 and Opera 12.16.
    Tested and works on Chrome 28.0 and IE 7,8.

    • Raghunath (Web Freelancer) says:

      Hi Petar,
      I am not sure for all latest browsers. these was working fine in all old browsers

  4. Ahsan says:

    Great Workaround …

  5. ghani says:

    this is not enough

  6. Mason says:

    Literally spent about an hour and a half trying to find a workaround and then I saw this…thank goodness

  7. Kapil says:

    NOT WORKING… !! Tested on Chrome 30, FireFox 24

  8. Denis says:

    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!!

  9. Very nice post. I certainly love this website. Thanks!

  10. Mayur says:

    Code not display on Mozilla, opera. I want code that work on any browser.

  11. Datta says:

    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

    • Hi,

      I am here little confuse so can you please explain your problem in details.

      Thanks
      Raghunath

      • Datta says:

        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

      • Thanks for quick reply!

        So problem is only coming in Google chrome?

  12. Pradeep says:

    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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s