window.focus() and blur() between tabs not working

Discuss how to use and promote Web standards with the Mozilla Gecko engine.
Post Reply
starr
Posts: 3
Joined: April 8th, 2009, 9:19 am

window.focus() and blur() between tabs not working

Post by starr »

Hello All,

I'm trying to build a data navigation utility that will allow a central "map" page to control a
number of tab pages that are loading individual pages from a large set of HTML.
All are local file://localhost/, with localhost popups enabled.
(for dev/testing all popups are enabled since firebug 1.3.3 has problems loading localhost
.js code)
preference to "raise and lower windows" has been enabled
I am using firefox 3.0.8 on FreeBSD 6.4

Anyway, I can't get window.focus() and blur() to work between cooperating tab/windows
who know each other's names.

Here following are two cooperating mirror tab pages that i'm trying to get working.
Have tried a number of combinations of the focus() and blur() calls.
Have tried with/without messaging. Messaging works fine.

I have noticed that the calling tab seems to unload/reload as a result
of the click event. This still allows the message to get through OK,
but I guess the reload could be taking the focus again.

An annoying alert() dialog could of course get focus to the message receiver,
but it requires another click to get it out of the way.

The concept of these focus() and blur() calls is so simple, and I've searched all
over the web for examples, but nothing seems applicable.

Have also seen that this may be a bug in Firefox.

Can anyone help me figure out what's happening and the right sequence
to make these calls work ?

======== code for "mirror" pages "focus1" and "focus2" follows:

Code: Select all


<html>
<head>
<title>test window focus - 1</title>
<script language="javascript">
var co_win2;
var co_name2 = "focus2";
function init_it() {
window.name = "focus1";
window.addEventListener("message", rcv, false);
}

function n() {
   // alert( "click on FCS1" );
    if( ! ( co_win2 && ! co_win2.closed ) ) { // reopen window
          co_win2 = open( "", co_name2, "" );
    }
    window.focus();
    window.blur();
    co_win2.postMessage( "go", "*" );
    //co_win2.focus();
    event.stopPropagation();
    event.preventDefault();
}

function rcv(evm) {
console.log( "1 received from 2" );
   window.blur();
   window.focus();
}

</script></head><body onload="javascript: init_it()">
<a id="FCS1" href="" onclick="n()">Focus other window</a>

</body></html>

================  focus2:

<html>
<head>
<title>test window focus - 2</title>
<script language="javascript">
var co_win1;
var co_name1 = "focus1";
function init_it() {
window.name = "focus2";
window.addEventListener("message", rcv, false);
}

function n() {
   // alert( "click on FCS2" );
    if( ! ( co_win1 && ! co_win1.closed ) ) { // reopen window
          co_win1 = open( "", co_name1, "" );
    }
    window.focus();
    window.blur();
    //co_win1.focus();
    co_win1.postMessage( "go", "*" );
    event.stopPropagation();
    event.preventDefault();
}

function rcv(evm) {
console.log( "2 received from 1" );
   window.blur();
   window.focus();
}

</script></head><body onload="javascript: init_it()">
<a id="FCS2" href="" onclick="n(event)">Focus other window</a>

</body></html>

Post Reply