Javascript Will bring a window to the front if it is already open in window.open?

If you open a window, for example:

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

If winName already open, it just changes the URL in the window. This is normal, but if this window is outside the current window, most users will not understand this and think that it simply does not open.

Is there a way that if a window is already open, it brings it to the fore?

+30
javascript
Jul 22 '10 at 16:52
source share
7 answers

Update . This did not work with Chrome (21+). The workaround is to close / open.

The window.open () method returns an object that represents a new window. You just need 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(); 
+45
Aug 24 '10 at 11:28
source share

Various answers suggesting using any form of .focus() will most likely not work in all browsers.

This one is used to work on the same day, but nothing more, mainly due to the fact that browsers actively stop shadow ad networks from pushing pop-up ads to the forefront.

Mozilla Firefox, in particular (depending on your version), has a configuration option that is enabled by default, which stops other windows (for example, pop-ups) from focusing.

This parameter can be found on the about:config page (test carefully!)

dom.disable_window_flip: true

If I remember correctly, this parameter was usually called ~ allow_raise_or_lower_windows *

Other browsers may implement something similar, but it’s quite simple if, by default, one of the main browsers blocks the use of .focus() , so there’s not much when trying to call it.

As a result, the only solution I've seen works is to see if the window exists and it is not closed yet ... and if it is close, load the desired window.

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

when you open a popup, if there is a chance that it is already open (and looped behind other windows), you can call this function before trying to open the popup.

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

Of course, the disadvantage is that if there was something “important” in this window (for example, partially filled), it will be lost.

+9
Jun 25 '14 at 20:51
source share

Update . This did not work with Chrome (21+). The workaround is to close / open.

Be careful, because when you open a new window, the window that opens may still have some code to execute, perhaps part of this code gives it focus. You will see how your new window opens in front and suddenly goes in the opposite direction, so in these cases it is a great idea to set a timeout, to focus a little on the new window, when all the javascript in the window that opens is done, you can do it as follows way:

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

Expect 1000 milliseconds to complete the name of the open window. You can also use this code in the window that opens, for example, when loading the body.

+7
Nov 20 '12 at 7:28
source share

I fixed it by adding

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

into the html element (button) and then change the url via JS.

+3
Dec 02 '14 at 5:59
source share

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

+2
Jul 22 '10 at 17:02
source share

You can use jQuery:

 myPopup = window.open(url, "Title","menubar=no, status=no, scrollbars=no, menubar=no, width=800, height=800"); $(myPopup).focus(); 
0
Jul 13 '16 at 15:27
source share

Closing the window first does the trick for me:

 window.open('', 'mypopup').close(); setTimeout(function() { window.open('', 'mypopup').focus(); }, 500); 
0
Sep 05 '18 at 5:08
source share



All Articles