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

Discussion in 'Javascript' started by drupel, Dec 27, 2005.

  1. drupel

    drupel Guest

    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----
     
    drupel, Dec 27, 2005
    #1
    1. Advertising

  2. drupel

    Lee Guest

    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?
     
    Lee, Dec 27, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    656
    Thomas Hansen
    Feb 15, 2007
  2. Piccolo Pete

    I can't regain control of my form

    Piccolo Pete, Apr 7, 2006, in forum: C++
    Replies:
    9
    Views:
    355
    Piccolo Pete
    Apr 8, 2006
  3. thomas
    Replies:
    0
    Views:
    229
    thomas
    Oct 23, 2003
  4. FAQ server
    Replies:
    4
    Views:
    270
    Dr J R Stockton
    Apr 25, 2007
  5. Roger
    Replies:
    3
    Views:
    336
Loading...

Share This Page