Cannot regain keyboard focus after closing childwindow (Firefox)...

D

drupel

Simple task:
A main window opens a "child" window.
Both windows have keydown handlers.
Main window opens child window on enter key.
Child window closes itself on enter key.

After the child window closes, the main window has lost keyboard focus,
meaning that it does not respond to keydown events anymore.

Installed a timer that checks periodically if child window is closed.
Setting focus back to main window in this timer did not succeed.
Tried self.focus() and window.focus().

Any suggestions?

Greetings,
drupel

Sample code follows:

----x---- window-opener.html begin ----x----
<html>
<head>
<title>window opener</title>
<script>
function log_writeln() {}

function test(e) {
// access event data
if( typeof( e ) == 'undefined' ) {
e = window.event;
if( typeof( e ) == 'undefined' ) {
alert( "e undefined" );
return;
}
}

var key;
// access key code
if( e.keyCode ) {
key = e.keyCode;
} else
if( e.which ) {
key = e.which;
} else {
log_writeln( "key undefined" );
return;
}

log_writeln( "key : " + key );
if( 13 == key ) {
enter();
window.setTimeout( "gainFocus()", 50 );
}
} /// method

var childWindow = null;
function enter() {
if( null != childWindow ) {
return;
}

childWindow = window.open( "/test/open-window.html",
"open_window", "width=100px, height=100px" );
} /// method

function gainFocus() {
log_writeln( "gain focus enter" );
if( null != childWindow && childWindow.closed ) {
//window.focus();
self.focus();
childWindow = null;
log_writeln( "gain focus OK" );
} else {
log_writeln( "gain focus trying" );
window.setTimeout( "gainFocus()", 50 );
}
} /// method
</script>
</head>
<body onkeydown="test(event);">
</body>
</html>
----x---- window-opener.html end ----x----

----x---- open-window.html begin ----x----
<html>
<head>
<title>open-window</title>
<script>
function test(e) {
// access event data
if( typeof( e ) == 'undefined' ) {
e = window.event;
if( typeof( e ) == 'undefined' ) {
alert( "e undefined" );
return;
}
}

var key;
// access key code
if( e.keyCode ) {
key = e.keyCode;
} else
if( e.which ) {
key = e.which;
} else {
alert( "key undefined" );
return;
}

if( 13 == key ) {
window.close();
} else {
alert( key );
}
}
</script>
</head>
<body onkeydown="test( event );">
</body>
</html>

----x---- open-window.html end ----x----
 
L

Lee

drupel said:
Simple task:
A main window opens a "child" window.
Both windows have keydown handlers.
Main window opens child window on enter key.
Child window closes itself on enter key.

Don't use the keydown event to do things that move the focus.
Why not onclick?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top