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

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

  • The first click opens a popup window.
  • For later clicks, if the popup is still open, just bring it to the front. If not, reopen.

The code below works in Firefox (Mac and Windows), Safari (Mac and Windows) and IE8. (I have not tested IE6 or IE7 yet.) However, on Google Chrome (on both Mac and Windows), later clicks did not bring the existing pop-up to the foreground 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, since I think my code solves the problem, at least for other modern browsers and IE8. If there is preferred etiquette for this, please let me know.

+12
javascript google-chrome popup
Apr 24 '10 at 5:16
source share
4 answers

You can not. Window.focus is disabled in Chrome for security reasons, and this is unlikely to change.

You need to close and update the corresponding window.

+8
Apr 24 '10 at 5:24 a.m.
source share

Why not just do popopWindow.focus () after calling window.open? It also does not hurt to do it there, and this should ensure that your new window is shown on top of the current one.

0
Apr 24 '10 at 5:19
source share

You need to set the original window to blur and a new window for focusing.

 var popup = { //popup = object literal representing your popup execute = (function () { popup.win = window.open(); popup.win.focus(); }()); }; window.blur(); 
0
Apr 24 '10 at 5:42 on
source share

The following code works for me when called in onclick handler:

 var popup = window.open('', 'popup-name', 'resizable,width=480=height=575'); if(popup.location == 'about:blank') { popup.location = 'http://google.com'; return; } 

This code works when trying to access a popup location if it is near: its new popup window is empty and the URL is set. Otherwise, if a window with the same name "pop-up name" is already open, the pop-up window is in focus. This code must be called from the onclick handler, otherwise it will not work.

0
Apr 30 '16 at 14:33
source share



All Articles