In Firefox, how do I bring an existing popup window with multiple tabs to the front using javascript

Posted by brahn on Stack Overflow See other posts from Stack Overflow or by brahn
Published on 2010-04-24T18:41:46Z Indexed on 2010/04/24 20:13 UTC
Read the original article Hit count: 182

Filed under:
|
|

I would like to have a button on a web page with the following behavior:

  • On the first click, open a pop-up.
  • On later clicks, if the pop-up is still open, just bring it to the front. If not, re-open.

The below code generally works in Firefox, Safari, and IE8 (see here for Chrome woes). However, I have found a failure mode in Firefox that I don't know how to deal with:

If for some reason the user has opened a second tab in the pop-up window and that second tab has focus within that window, the popupWindow.focus() command fails to have any effect. (If the first tab has focus within that window, everything works just great.)

So, how can I focus the popup and the desired tab in Firefox?

<head>
  <script type="text/javascript">
    var popupWindow = null;
    var doPopup = function () {
      if (popupWindow && !popupWindow.closed) {
        popupWindow.focus();
      } else {
        popupWindow = window.open("http://google.com", "_blank",
          "width=200,height=200");
      }
    };
  </script>
</head>

<body>
  <button onclick="doPopup(); return false">
    create a pop-up
  </button>
</body>

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about popup