Skip to content

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!

Be Sociable, Share!

11 Comments

  1. Brian wrote:

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

    Tuesday, March 17, 2009 at 10:57 am | Permalink
  2. hazg wrote:

    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 ))))

    Tuesday, September 1, 2009 at 10:23 am | Permalink
  3. Djaity wrote:

    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.

    Wednesday, September 30, 2009 at 5:47 am | Permalink
  4. ritwick wrote:

    Works in:
    IE 6+
    Firefox 3
    Chrome 3

    Does not work in:
    Safari 3.1
    Opera 9.63

    Thursday, December 10, 2009 at 3:19 pm | Permalink
  5. emurmur wrote:

    Works for me in

    IE8
    FireFox 3.5.8
    Windows Safari 4.0.4
    Chrome 4.1

    Does not work in Opera 9.64

    Sunday, February 28, 2010 at 5:26 pm | Permalink
  6. Robert wrote:

    Thanks a lot, work fine for me!

    Sunday, May 2, 2010 at 7:39 am | Permalink
  7. Karandeep Singh wrote:

    var nextObject = document.getElementById(“select_combo_box”);
    nextObject.focus();
    ———————————————–
    that example is not working in Chrome and safari.
    Please give correct example for it that can run on every browser using Javascript or jQuery or MooTools

    Wednesday, May 26, 2010 at 1:46 am | Permalink
  8. Nick wrote:

    This method (to call blur() then focus() ) does not work for me on Chrome 5.0.375.126 on Mac OS X.
    Any workarounds found for Chrome on Mac?

    Monday, August 16, 2010 at 3:05 pm | Permalink
  9. kent wrote:

    @Nick: The original method (closing, then opening) in the post above does work on Chrome 5.0.375.126 on Mac OS X. I haven’t tried the alternate methods proposed…

    Monday, August 16, 2010 at 3:22 pm | Permalink
  10. LeFe wrote:

    Call nameOfVarWindow.blur(); before nameOfVarWindow.focus();

    Took the solution from here: http://stackoverflow.com/questions/2758608/window-focus-not-working-in-google-chrome

    Friday, October 15, 2010 at 8:08 am | Permalink
  11. simoncpu wrote:

    As of Chrome 13.0.782.112, widow.parent.blur() minimizes the window… Aaaaaargh… :-/

    Sunday, September 4, 2011 at 11:38 am | Permalink