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.

Click here to read more

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 to Kapil Cancel reply

Recent Posts