How do I set focus back to the parent window when in another browser tab? I've tried:
window.opener.focus();
to no avail. It only seems to work when the windows are not docked.
I need this to work in Chrome, IE9/10, Firefox, and Safari.
Internet Explorer also seems to have issues setting focus to a child window. E.g. when using:
var windowRef = window.open(url);
then later from the same tab:
windowRef.focus(); // ok in Chrome, doesn't seem to work in IE...
focus() method. Your child window can refer to its parent window with 'opener' - so the command in the child window would be opener. focus(), or window. opener.
opener. The Window interface's opener property returns a reference to the window that opened the window, either with open() , or by navigating a link with a target attribute. In other words, if window A opens window B , B. opener returns A .
focus() Makes a request to bring the window to the front.
What exactly do you want to achieve? To set the focus inside the other window to the first link / button / form field / whatever? Or to make the other window (browser tab) the active browser tab?
Anyways - maybe the opener.focus()
call doesn't work because both windows are not served from the same origin. Try setting up a communication channel via postMessage to resolve this. So in the opener
document, listen for an event like this:
window.addEventListener('message', function (event) {
window.focus();
}, false);
And in the opened window, send the message like this:
opener.postMessage('foo', '*');
Sadly, I can't test if this is working right now. (I guess it should, though...)
A few things to add, though:
attachEvent
equivalent.'*'
origin wildcard with the actual origin of the opener window.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With