Currently, window.focus can be used to shift the focus to another tab or window, even if this is not user-initiated. This can cause a jarring user experience so should be disallowed. Note that Firefox disables window.focus() when called from a page's onload handler.
The fix for this will require the ResourceRequest 'userGesture' flag being added in Bug 33381
(In reply to comment #1) > The fix for this will require the ResourceRequest 'userGesture' flag being > added in Bug 33381 Why is that? Could't we just query whether a userGesture (ala popup detection) was taking place when calling window.focus()?
Created attachment 64522 [details] Attempt at a patch
Comment on attachment 64522 [details] Attempt at a patch This needs tests. r-.
I was not sure on how to simulate user gesture in a layout test. The LayoutTestController does not seem to have anything to that effect. Do you have any pointers?
In most cases, one can simulate a user gesture with eventSender. For this to go in, we'd need a more extensive research of compatibility issues. Per the description, the proposed behavior doesn't even match Firefox. Are we sure that focusing windows outside of a user gesture is never good?