Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.opener.focus() doesn't work

I can't seem to get this to work.

In response to a click, window A opens window B (which then has focus). Then, in response to a click on B, the window calls window.opener.focus(), but the focus does not go back to A.

I have found a strange, strange workaround for Chrome (29, possibly others). If I run:

window.opener.name = 'somename';
window.open(window.opener.location.href, window.opener.name);
window.opener.focus();

it does work (and doesn't reload window A). But this doesn't work for Firefox, and it is probably a fluke anyway.

It seems very clear to me what opener and focus are supposed to do, but window.opener.focus() doesn't work. What am I missing?

like image 909
Paul Draper Avatar asked Aug 29 '13 01:08

Paul Draper


People also ask

How to set focus on popup window in JavaScript?

setTimeout(function () { window. open('PageName', '', 'height=560,width=1120,resizable=no,left=100,top=50,screenX=100,screenY=50'); }, 100); -> This code is use for giving focus on newly open window. -> Put it in javascript.

What is window focus in Javascript?

focus() Makes a request to bring the window to the front.

What is window opener in JavaScript?

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 .


1 Answers

From the fine manual:

Makes a request to bring the window to the front. It may fail due to user settings and the window isn't guaranteed to be frontmost before this method returns.

Emphasis mine. Calling focus() is just a request and the browser is free to ignore you and you should generally expect to be ignored. Consider what sorts of nefarious things you could get up to by switching focus to a tiny window while someone is typing if you need some reasons why a browser would ignore your request.

If you need focus() to work for your application to work then you need to redesign your application so that it doesn't need to call focus().

like image 183
mu is too short Avatar answered Nov 15 '22 14:11

mu is too short