Categories
Bugs Development JavaScript Technical

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 replies on “Chrome problems with window focus (workaround)”

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

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.

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

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?

@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…

Comments are closed.