38

If you open a window like:

window.open ("url","winName","location=0,width=300,height=214");

If winName is already open it just changes the URL in the window. This is ok but if that window is behind the current window most users won't realize this and think that it is just not opening.

Is there any way that if the window is already open, it brings it to the front?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
JD Isaacks
  • 56,088
  • 93
  • 276
  • 422
  • possible duplicate of [Using Javascript, How do I bring an already existing open window to the front on top of other windows from another windows code?](http://stackoverflow.com/questions/2530572/using-javascript-how-do-i-bring-an-already-existing-open-window-to-the-front-on) – Andy E Jul 22 '10 at 17:01
  • 2
    @Andy in that question the user is asking how to bring the opener/parent window to the front from the child/opened window. I am asking how to do it in reverse. Bring the child/opened window to the front from the parent/opener window. If that makes sense. – JD Isaacks Jul 22 '10 at 17:04
  • sorry. I have seen this question before, though - I just can't find it now :-) – Andy E Jul 22 '10 at 17:05

9 Answers9

52

Update: This hasn't worked since Chrome (21+). The workaround is to close/reopen.

The window.open() method returns an object that represents the new window. You just need to window.focus() it:

var w = window.open ("url","winName","location=0,width=300,height=214");
w.focus();

Or simply:

window.open("url","winName","location=0,width=300,height=214").focus();
Michael Cole
  • 15,473
  • 7
  • 79
  • 96
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 3
    This will only work in IE if the popup window doesn't have other browser tabs open in the same application host. Even if the popup window has focus over the other tabs, it will just silently fail in IE9, probably older versions too. Fortunately(?), unhinging the popup tab from a group of tabs retains the correct host window reference, and it will resume behaving as expected. – Marcus Pope Feb 12 '13 at 20:29
11

The various answers suggesting using any form of .focus() are likely not going to work in all browsers.

This used to work back in the day but not any more, mainly due to browsers working to actively stop shady ad networks from pushing their popup ads to the foreground.

In Mozilla Firefox in particular (depending on your version) there is a configuration setting that is turned on by default that stops other windows (e.g. popups) from focusing themselves.

You can find this setting in the about:config page (tread carefully!)

dom.disable_window_flip: true

If I recall correctly this setting used to be called something like ~"allow_raise_or_lower_windows*

Other browsers may implement something similar, but quite simply if 1 of the major browsers blocks the use of .focus() by default then there's not much use in attempting to call it.

As a result, the only solution I've seen that works is to see if the window exists, and is not already closed... and if so close it, then load the window you want.

function closePopupIfOpen(popupName){
  if(typeof(window[popupName]) != 'undefined' && !window[popupName].closed){
    window[popupName].close();
  }
}

when opening your popup if there's a chance it is already open (and burried behind other windows) then you can call this function before you attempt to open your popup.

closePopupIfOpen('fooWin');
var fooWin = window.open('someURL', 'foo', '...features...');

The drawback of course is that if there was anything "important" (e.g. a form partially filled in) in that window it will be lost.

scunliffe
  • 62,582
  • 25
  • 126
  • 161
  • works in chrome but doesn't look like it has any effect in IE11 – Samuel Sep 29 '14 at 17:14
  • 1
    this worked for me in Chrome (70), but not FF (63). How I made your script work was by defining the window as a global variable instead of scoped to the method. I hope this helps someone else down the road. – fizch Nov 29 '18 at 17:51
8

Update: This hasn't worked since Chrome (21+). The workaround is to close/reopen.

Be careful, because when you open a new window, the opener window might still have some code to be executed, maybe some of this code gives it the focus. You would see how your new window opens in the front and suddenly goes to the back, so, it is a great idea in these cases, to set a timeout in order to give the focus to the new window a bit later on, when all the javascript in the opener window is executed, you can do it this way:

    setTimeout(function(){window.focus();},1000);

Being 1000 the amount of miliseconds to wait, and window the name of the opened window. You could also use this code in the opened window in the body onload for example.

Michael Cole
  • 15,473
  • 7
  • 79
  • 96
nahiko
  • 127
  • 1
  • 7
3

I fixed this by adding

onclick="myWin = window.open('','winName','location=0,width=300,height=214'); myWin.focus()"

to the html element(button) and then change the URL via JS.

Raffy Cortez
  • 458
  • 3
  • 8
2

window.focus() applied to the window in question should do the trick.

Mrchief
  • 75,126
  • 20
  • 142
  • 189
Robusto
  • 31,447
  • 8
  • 56
  • 77
  • Can you give me an example I tried: `editDaySched = window.open(...); editDaySched.focus();` in Chrome but it didn't seem to work. Thanks. – JD Isaacks Jul 22 '10 at 18:08
  • If you are calling focus() inline like that it won't work because the window doesn't exist yet. What you should do is have a function in the opened window (here editDaySched) that calls window.focus() when the onload event fires. You can do a proof of concept from the opener with a setTimeout focusing the editDaySched window after, say, 5 or 6 seconds. – Robusto Jul 22 '10 at 18:32
0

You can use jQuery :

myPopup = window.open(url, "Title","menubar=no, status=no, scrollbars=no, menubar=no, width=800, height=800");
$(myPopup).focus();
  • 3
    jquery focus is just a wrapper for the standard focus() method so add's no benefit. Also the OP didn't ask for a jQuery solution – Liam Jul 13 '16 at 15:30
0

Closing the window first, does the trick for me:

window.open('', 'mypopup').close();
setTimeout(function() {
    window.open('', 'mypopup').focus();
}, 500);
appcropolis
  • 406
  • 4
  • 7
0

I had the same problem, have spent a lot of time to find a solution, finally it works by this:

var singleWindow;
function openNewWindow(){
 if(singleWindow){
  singleWindow.close();
 }
 singleWindow = window.open ("url","winName","location=0,width=300,height=214");
}
This close the current window (if exist) and open a new window that will be placed on the top (default behavior)

Hope this help you !

Shessuky
  • 1,846
  • 21
  • 24
0

You can try this, no need for something extra.

Tested and working on all browsers (Including Netscape):

newWin = window.open("url","winName","location=0,width=300,height=214");
if(window.focus) {newWin.focus()}
ja0
  • 37
  • 4