Closing a Firefox Window with Javascript

I recently had a problem where I was opening a browser window from an e-mail client. After taking a survey, I wanted a button to close this same window. In IE, it was fine to simply use window.close(); to close the window. However, Firefox, the evil genius that it is, will not let you do that because it believes you did not click to open this window in the first place. In order to trick it, you quick load a blank page then close it. The code:

<a href="javascript:window.open('','_parent','');window.close();">
Close this window</a>

So now, when you click the link, it closes the browser window. It works in both IE and Firefox 1.5 (and I believe others as well, but not personally tested). Finding that snippet of code saved me a lot of time! So I hope someone else finds it useful.

Update, May 1 2008:

This code only works on Firefox 1.5 and IE. In Firefox 2.0, Unless a website opened up a window – it can not close it. So, in my case I want to close the window after a user takes a survey, opened from an email window. Firefox 2.0 gives this error: Scripts may not close windows that were not opened by the script. I’m not sure what the right answer is here. They are doing it to prevent malicious scripts from closing windows. But even if they added an option to change that, most users wouldnt have it enabled by default.

So… where does that leave us? Please let me know if anyone knows of code that can close a window with Javascript in Firefox 2.x or 3.x.

Share and Enjoy:
  • Twitter
  • del.icio.us
  • Digg
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • StumbleUpon

No Trackbacks

20 Comments

  1. Lorenzo

    Bravo!!! Funziona.

    Posted November 8, 2006 at 5:23 am | Permalink
  2. Doesn’t work in firefox 2.0

    Posted December 14, 2006 at 7:14 am | Permalink
  3. Does anyone have a solution to Firefox 2.0 and closing a window with Javascript that does not involve changing Firefox settings?

    Posted December 20, 2006 at 1:16 pm | Permalink
  4. MH

    But this trick is not working for FF 2.0.0.2, do you have any work around for this?

    Posted March 7, 2007 at 4:59 am | Permalink
  5. Laurie

    did anyone ever figure out how to write a close window javascript that will work in Firefox 2?

    Posted October 2, 2007 at 3:41 pm | Permalink
  6. Nikolai

    This worked perfectly for me with Firefox 2.0.0.9

    Posted November 14, 2007 at 2:20 am | Permalink
  7. JohnsonS

    Not working with Firefox 2.0.0.10

    Posted November 29, 2007 at 6:43 pm | Permalink
  8. kecik

    not working with my Firefox 2.0.0.9

    Posted December 3, 2007 at 4:34 pm | Permalink
  9. Lydia

    Not working with my Firefox 2.0.0.12.

    Posted February 27, 2008 at 4:18 pm | Permalink
  10. berkx

    Not working with firefox v2.0.0.12

    Posted March 3, 2008 at 3:39 am | Permalink
  11. Not working with firefox v2.0.0.12

    Posted March 3, 2008 at 4:19 am | Permalink
  12. tester

    Not working with my Firefox v2.0.0.14.

    Posted May 1, 2008 at 8:43 am | Permalink
  13. vishal

    its not working on FireFox 2.0

    Posted May 9, 2008 at 5:29 am | Permalink
  14. forex

    under ff 2 you have to set manually under about:config
    dom.allow_scripts_to_close_windows to true
    then it’ll work

    Posted May 21, 2008 at 11:46 am | Permalink
  15. Alexia

    Works like a charm! Thanks!!!:)

    Posted May 29, 2008 at 4:45 am | Permalink
  16. Hussain

    well it works with older version .. thankyou

    Posted August 18, 2008 at 2:39 am | Permalink
  17. Robin

    it doesn’t work in FF2.0.0.13

    Posted September 6, 2008 at 6:24 pm | Permalink
  18. Cecilia

    This works in firefox 2.0.0.17 (for Mac):

    Use this script to open a popup window:

    then to close window: insert the above script before the close link on popupfile.html, then make link:

    Posted October 10, 2008 at 11:02 am | Permalink
  19. Cecilia

    <script type="text/javascript" language="JavaScript">
    <!–
    function PopupWindow() {
    var attributes = ‘width=400,height=500,scrollbars=yes’;
    window.open(‘XXXXXXXXXXXX.html’,’2′,attributes);
    } //–>
    </script>
    <a href="javascript:PopupWindow()"><img src="xxxxxxxxxxxxxx.jpg" border="0"> </a>
     
     
    then to close: script before link, then make link:
     
    <a href="javascript:window.close()">

    Posted October 10, 2008 at 11:09 am | Permalink
  20. rick

    Thanks Guys! Just learning js and found your solution for firefox. Now if I can only put my hair back in my head.

    Posted December 24, 2008 at 12:41 am | Permalink