Javascript Bring window to front if already open in

If you open a window like: ("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?



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

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

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

Or simply:"url","winName","location=0,width=300,height=214").focus();

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

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.

var fooWin ='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.


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:


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.


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


I fixed this by adding

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

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


You can use jQuery :

myPopup =, "Title","menubar=no, status=no, scrollbars=no, menubar=no, width=800, height=800");

Closing the window first, does the trick for me:'', 'mypopup').close();
setTimeout(function() {'', 'mypopup').focus();
}, 500);

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

var singleWindow;
function openNewWindow(){
	singleWindow = ("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 !


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.