Thursday 15 September 2011

javascript - How to set focus to child and opener windows when tabbed -


How can I refocus focus on any other browser tab? I have tried:

  window.opener.focus ();   

No use it works only when the window is not docked.

I need it to work in Chrome, IE9 / 10, Firefox, and Safari.

Internet Explorer also has problems setting the focus in the hair window. Like when using:

  var windowRef = window.open (url);   

Then from the same tab later:

  windowRef.focus (); // Right in Chrome, does not seem to work in IE ...    

What do you want to achieve? Focus inside to set another window on the first link / button / form field / whatever? Or other windows (Browser tab) to create active browser tabs?

Anyway - opener.focus () The call does not work because the two windows do not serve the same root try to set up a communication channel to resolve it so In the Openers document, listen to an event like this:

  window.addEventListener ('message', function (event) {window.focus ();} , false);   

and in the open window, send the message like this:

  opener.postMessage ('foo', '*');   

Unfortunately, I can not test if it's still working. (I think it should be, though ...)

To add some things, though:

  1. Support some legacy crap browsers (see) message Do not do it. The proceedings are, though. :)
  2. I used W3 standard notation to add the event listener. Use IE8 and below to their proprietary attachEvent equals.
  3. You should change the original wildcard with
    '*' with the original origin of the opener window.
  4. In the message Event Listener function, you should inspect the event.origin and if the original is correct then only run your actual payload.

No comments:

Post a Comment