views:

147

answers:

1

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

I can't comment so I use the answer box. Is the page you are popping from your own domain? If so, can you put a

window.onload=function() {
  window.focus();
}

in there to see if it changes the situation?

mplungjan
Sorry, I don't think this addresses the problem. When I first open the popup window everything works just fine. My question is about attempting to focus the popup after it has already been opened.
brahn
I understand fully. If what I suggested does not work, then I would think you can forget it.
mplungjan