In Google Chrome, how do I bring an existing popup window to the front using javascript from the parent window?
12,021
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.
Author by
brahn
Updated on September 16, 2022Comments
-
brahn over 1 year
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.
-
brahn about 14 yearsThe current code results in the desired behavior (i.e. popup on top) with the new window -- the problem is with an existing window.
-
brahn about 14 yearsOof, that's frustrating. Out of curiousity, what is the security concern? I see some discussion of what might be related reasoning here, though that seems more about the popup stealing focus from the parent window rather than the parent window giving focus to the popup (which is what I'm looking for). Thanks!
-
marquito about 10 yearsI'd also like to know about the concerns involved.
-
Nick Rice about 8 yearsAfter all these years this is still a major frustration as a user, let alone a developer. It would be good if someone would provide a link to something that nicely explains the reason because as-is "for security reasons" sounds like train companies saying "for the benefit of passengers" when they impose some expedient regulation that patently isn't.
-
Michael Cole over 7 yearsMost basically, because a-holes will pop windows in front to get advertising clicks. Until the web isn't driven by ads, this is the web we live with.
-
Stefan Steiger over 7 years@Michael Cole: Amen ;) And not only that - they can keep it in front and you're unable to close it, and it repopens when you close the window - until you click that f*ing add (or malware).