Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Google Chrome, how do I bring an existing popup window to the front using javascript from the parent window?

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 works in Firefox (Mac & Windows), Safari (Mac & Windows), and IE8. (I have not yet tested IE6 or IE7.) However, in Google Chrome (both Mac & Windows) later clicks fail to bring the existing pop-up to the front as desired.

How can I make this work in Chrome?

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

Background: I am re-asking this question specifically for Google Chrome, as I think I my code solves the problem at least for other modern browsers and IE8. If there is a preferred etiquette for doing so, please let me know.

like image 451
brahn Avatar asked Apr 24 '10 05:04

brahn


People also ask

How do I open a popup with JavaScript?

The syntax to open a popup is: window. open(url, name, params) : url. An URL to load into the new window.

What are the two pop up windows in JavaScript?

JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.


1 Answers

You can't. Window.focus is disabled in Chrome for security reasons, and that is unlikely to change.

You have to close and repopen the respective window.

like image 118
Stefan Steiger Avatar answered Oct 22 '22 13:10

Stefan Steiger