The question was fairly descriptive but I'll describe it further.
Basically, I have window1
. Clicking a button link opens window2
. Clicking a button in window2
opens window3
, clicking a button in window3
should bring window2
back to the front of the screen on top of window2
.
I'm not sure how this is exactly done, however I have used and played around with focus(), opener and other various methods and I cannot seem to get it to work properly.
The question was fairly descriptive but I'll describe it further.
Basically, I have window1
. Clicking a button link opens window2
. Clicking a button in window2
opens window3
, clicking a button in window3
should bring window2
back to the front of the screen on top of window2
.
I'm not sure how this is exactly done, however I have used and played around with focus(), opener and other various methods and I cannot seem to get it to work properly.
Share Improve this question edited Jul 11, 2021 at 16:28 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 27, 2010 at 19:09 user268249user268249 411 gold badge1 silver badge2 bronze badges 3- Quick solution, don't use popups and instead handle it inside the page using layers and a UI library (e.g. jQuery UI Dialogs). Popups are ultra-annoying in this day and age. – Max Shawabkeh Commented Mar 27, 2010 at 19:14
- possible duplicate of Javascript Bring window to front if already open in window.open? – GOTO 0 Commented Nov 19, 2013 at 14:18
- @MaxShawabkeh, sometimes we don't have alternatives, opening on a modal/dialog prevent user from navigate on site if they aren't on a reactive website or SPA. – Giovan Cruz Commented Apr 28, 2020 at 18:37
2 Answers
Reset to default 1Update: This hasn't worked since Chrome (21+). The workaround is to close/reopen.
The following code works for me on Firefox (Mac & Windows), Safari (Mac & Windows), and IE8 (Windows, of course). I haven't tested IE6 or IE7.
However, it does not work on Chrome for either Mac or Windows. Specifically, clicking the button once creates the pop-up and brings it to the front. However, returning to the original window and clicking the button again does not refocus the popup.
<head>
<script type="text/javascript">
var popupWindow = null;
var doPopup = function () {
if (popupWindow && !popupWindow.closed) {
popupWindow.focus();
} else {
popupWindow = window.open("http://google.", "_blank",
"width=200,height=200");
}
};
</script>
</head>
<body>
<button onclick="doPopup(); return false">
create a pop-up
</button>
</body>
Update: This hasn't worked since Chrome (21+). The workaround is to close/reopen.
opener.focus()
does work. If it doesn't for you, we'll need a test case.
Some things that might cause problems: calling it in an event handler that fires before the button's window gets focus due to the click (but I don't think that'd usually be the case); running it on a browser that stuffs pop-ups into browser tabs instead.
(I agree with Max's ment. Pop-ups with cross-window scripting are generally best avoided.)