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!

11 Comments