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:


Close this window

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.

46 comments
  1. Does anyone have a solution to Firefox 2.0 and closing a window with Javascript that does not involve changing Firefox settings?

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

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

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

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

  6. 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:

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

  8. <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()">

  9. <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()">

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

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

  12. Please let me know if anyone knows of code that can close a window with Javascript in Firefox 2.x or 3.x.

  13. window.focus(); window.close(); should work in FF 2.0+

    FF is MUCH more JS friendly then IE

  14. please set your firefox browser:

        1.input “about:config ” to your firefox address bar and enter;

        2.make sure your “dom.allow_scripts_to_close_windows” is true

Comments are closed.

You May Also Like

Send your cell phone SMS system alert messages.

SMSSend is a program used to send SMS messages over the network…

How to Install SNMP on Tomato Router Firmware and Graph Traffic with Cacti

You’ve flashed your old WRT54G or other vanilla router with the Tomato…

Adding Random Quotes to the Bash Login Screen

According to “official” system administrator rules and guidelines you shouldn’t be adding…

Installing And Compiling Zabbix Client / Agent

Zabbix is an excellent system monitoring package. It does everything from basic…