Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Bring window to front if already open in window.open?

Tags:

javascript

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?

like image 725
JD Isaacks Avatar asked Jul 22 '10 16:07

JD Isaacks


People also ask

How you can open a new window on top of other windows by calling?

Otherwise the call to window. open() will just create a new window. To open a new window on every call of window. open(), use the special value _blank for strWindowName.

How do you check if a window is already open in JavaScript?

How to check if an opened browser window is closed or not in JavaScript? To check if an opened browser window is closed, you can use the closed property in referenced window object in JavaScript. The property returns a boolean true if the window is closed and false if the window is in the opened state.

Can you open multiple windows at once in JavaScript?

The open() method to opens a new browser window, or multiple new tab in JavaScript.

How do you target a window in JavaScript?

Use top. location. href and other link targets in Java But if the link is defined to open in a new window, it will appear in a new window or tab on your browser. If the link is defined to open in a new frame, it will pop up on top of the current page in your browser.


5 Answers

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();
like image 76
Álvaro González Avatar answered Oct 06 '22 00:10

Álvaro González


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.

like image 42
scunliffe Avatar answered Oct 06 '22 00:10

scunliffe


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.

like image 38
nahiko Avatar answered Oct 06 '22 01:10

nahiko


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.

like image 41
Raffy Cortez Avatar answered Oct 06 '22 01:10

Raffy Cortez


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

like image 41
Robusto Avatar answered Oct 06 '22 01:10

Robusto