Chrome problems with window focus (workaround)

Found a workaround for a window focus problem with Google’s Chrome browser.

Summary:

var w = window.open(url, ‘windowname’, ‘…’);
w.focus();

Problem is that the “focus” call doesn’t work … the 2nd time you call this function.

The workaround requires you to retain a pointer to the window as a global, and just close it before opening it again, like so:

if (navigator.userAgent.indexOf('Chrome/') > 0) {
    if (window.detwin) {
        window.detwin.close();
        window.detwin = null;
    }
}
window.detwin = window.open(URL,  'windowname', '...');
window.detwin.focus();

Hard-coded it for Chrome as it works in all other browsers, except for Chrome.

The bug on the Google site references a potential issue with Webkit, but the latest version of Safari doesn’t have the issue at all.

Comment if this helped you!

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Reddit
  • TwitThis

5 Comments »

  1. Brian Said,

    March 17, 2009 @ 10:57 am

    Google chrome is light and fast, but it seems like it’s having some problem with javascripts.

  2. hazg Said,

    September 1, 2009 @ 10:23 am

    u can do some thing like this:

    in parent:

    var w = window.open(href,’name’);
    if(w.focus)
    w.focus();

    in child window:

    if(window.parent && (navigator.userAgent.indexOf(‘Chrome/’) > 0)
    window.parent.blur();

    This method faster, and u can open focus window many times without storing var w = …

    sorry for my engliache ))))

  3. Djaity Said,

    September 30, 2009 @ 5:47 am

    Instead of closing the popup, then open a new one,
    I’ve found out the following sequence was also fixing the Chrome (WebKit) bug.

    if(typeof(win)!=’undefined’ && !win.closed) {
    win.blur();
    }
    else {
    win = window.open(mypage_location, mypage_name, mypage_option);
    }
    win.focus();

    In other words, if you use blur() on the window first then focus() function works.
    Note: We’re currently using Chrome 3.0.195.21 / working on XP.
    I haven’t tested yet on Safari and/or other platforms, any feedbacks on other platforms will be well-appreciated.
    Thanks.

  4. ritwick Said,

    December 10, 2009 @ 3:19 pm

    Works in:
    IE 6+
    Firefox 3
    Chrome 3

    Does not work in:
    Safari 3.1
    Opera 9.63

  5. emurmur Said,

    February 28, 2010 @ 5:26 pm

    Works for me in

    IE8
    FireFox 3.5.8
    Windows Safari 4.0.4
    Chrome 4.1

    Does not work in Opera 9.64

Leave a Comment