1

I have a problem with focusing between browser windows/tabs. I would be very grateful for some input! :-)

Our main target browser is Chrome. And I know that the rules for what's allowed has changed over the years - because these features have been constantly abused... I've looked at a lot of older answers, e.g. I need to open a new window in the background with JavaScript, and make sure the original is still focused, but I haven't found anything that will work today. (Apparently this is called a "pop-under" window.)

In our application, in window-A, we want to open another window/tab, window-B, with a certain URL. But the focus should remain on window-A. We have full control of the webpages that are loaded in both windows, and they are loaded from the same domain.

This won't work (in window-A):

window.open('mydomain.com/second-page');
window.focus();

And this won't work (in window-B, after load):

window.opener.focus();

For a number of years now, Chrome hasn't allowed a window to set focus to itself or its parent.

This will actually work:

// in window-A:
window.name = 'HOWDY';
window.open('mydomain.com/second-page');

// in window-B:
window.open('', 'HOWDY');

Opening a blank URL in a named windows, will switch focus to that window - without changing the contents of the target window.

BUT it only works if I put it in an event handler - e.g.:

document.getElementById('mybutton').addEventListener('click', function(e) {
    window.open('', 'HOWDY');
});

If I try to do it automatically (after load, or by sending the page a SignalR message), the open-blank-URL-in-named-window trick won't change focus. I suppose it's one of those "only allowed in direct response to user interaction" things. (It will load another page in window-A if I add a URL, though, so the open() call works. But window-B retains focus.)

Is there any way to solve this? Either by not moving focus to window-B in the first place, or by automatically moving focus back from window-B to window-A?

/Anders from Sweden

UglySwede
  • 429
  • 1
  • 7
  • 16

0 Answers0