Tuesday, February 03, 2009

How to prevent a web site from stealing the focus in Firefox

At work, for a while now, I've had my Firefox 3 browser homepage set to a particular page in our internal Confluence wiki.  This page is specially designed to be a "start page," consisting solely of a series of links to commonly-used internal applications and documentation, plus a few search fields allowing common searches to be performed (of the wiki; of the employees database; of the Internet via Google).  I also have the New Tab Homepage Firefox add-on installed, so this homepage appears when I open a new tab in Firefox.

This setup works great, with one glaring exception: The page takes a couple of seconds to load, and Confluence automatically sets the focus to the first search field on the page when it completes loading.  The result of this is whenever I opened a new browser window or browser tab and started typing a URL in the address bar to manually navigate to a page (which I actually do fairly frequently), typically halfway through my typing in the address bar, the web page would steal the focus, and the second half of the URL I was typing would appear in the search field on the page instead of in the address bar.

This problem isn't necessarily limited to a Confluence page set as a browser homepage; it could potentially occur with any web page that is coded to set the focus to a particular UI element on the page, such as the Google homepage.  Here's what might happen if I opened up a new browser window with the homepage set to Google, and started trying to type "http://localhost/" in the address bar as the page loaded:

browserStealFocus

Note that half of the "http://localhost" text appears in the address bar, but (in this example) after the "a" was typed the script web page made a call to the Javascript focus() method to set the focus to the search field, and Firefox honored this; as a result, the remainder of the typing appears in the search field -- the Google page managed to steal the focus.

I went about searching for a solution to this problem, and ended up finding one in a post on Kam-Hung Soh's blog.  Kam-Hung's post has instructions on how to disable a particular Javascript method for a particular web site. 

I followed the instructions, and added the following to my Firefox user.js file:

user_pref("capability.policy.policynames", "noinputfocus");
user_pref("capability.policy.noinputfocus.sites", "www.google.com");
user_pref("capability.policy.noinputfocus.HTMLInputElement.focus", "noAccess");

(In my case, I used the URL of my team's internal Confluence wiki site instead of "www.google.com").

After doing this, I was all set!  I no longer had my focus stolen after opening a new browser window or a new tab. (Thanks, Kam-Hung!)

Obviously, these instructions are Firefox-specific, and therefore aren't helpful for addressing this issue in other browsers.  However, when I tried to reproduce the issue in Internet Explorer 7, I was unable to do so -- in cases where I had started typing in the address bar as IE7 loaded the page, the web page never gained the focus.  Perhaps IE has some intelligent logic where it will ignore focus() calls made by the web page in the case where the user has already started typing something in the address bar?  It would be really nice to see all browsers implement such a feature, to avoid the need for workarounds such as the one described in this post.

Update 2/4/2009: I took a look, and there is in fact a bug in the Firefox Bugzilla database for this issue. It was initially reported back in 2002, though, so I'm not super optimistic about the bug being fixed in the near future. :-)

9 comments:

  1. Boy do I ever wished this worked, because my home page is set to http://www.google.com and it drives me bonkers to always be doing Google "searches" on the last half of the URL I'm trying to input into the address bar of a freshly-opened browser or tab. Unfortunately, it just doesn't work.

    I'm running the most current version of Firefox (3.5.2) on Windows XP, but I've apparently had this exact snippet in my user.js for quite some time and it simply fails to do anything at all.

    It was annoying me more than usual this morning, so I googled the issue, found this page (for the second time, apparently), went to input this into my user.js and lo-and-behold, it was already there. Then I remembered going through this process a few months back and giving up after it failed to work then...

    My user.js is in the correct location for Windows XP, etc. It just doesn't work for me. Such a pity, because this issue truly annoys me.

    ReplyDelete
  2. What does work, however, is the first method listed on the blog you linked to, which just disallows this behavior by default:

    user_pref("capability.policy.default.HTMLInputElement.focus", "noAccess");

    Works like a charm. I'm not going to tweak it any further, as I have no use for the focus-stealing behavior in the first place.

    ReplyDelete
  3. Hopefully we'll see a fix for this issue before too long in Firefox. There's been quite a bit of development activity on this bug recently (per the Bugzilla link near the end of the original post above).

    ReplyDelete
  4. Thanks! My home page stealing the focus was making me nuts.

    ReplyDelete
  5. I use firefox 3.5.7 and also cannot get the fucking thing to block only specific sites. i was able to get it to block everything as confirmed above, but that's fucking burning the goddamn house down to get rid of the fucking roaches!!!

    why doesnt this fucking shit work!!!! i only want to make fucking yahoo not steal my fucking focus!!

    http://forums.mozillazine.org/viewtopic.php?f=38&t=1801495&p=8982345#p8982345

    ReplyDelete
  6. What am I doing wrong? I put those 3 lines into a notepad document using www.yahoo.com, named it user.js and put it in E:\Program Files\Mozilla Firefox\defaults\profile\ and it still isn't working. Am I doing something wrong?

    ReplyDelete
  7. @Anonymous (12/25), I'm not sure you're putting the user.js file into the correct folder. Make sure you're following the instructions here:

    http://kb.mozillazine.org/Profile_folder_-_Firefox#Using_the_Help_menu_-_Firefox_3.6_and_above

    ReplyDelete
  8. Just an update: You can easily disable the "steal focus" behavior by going to "about:config", and changing "browser.autofocus" to false.

    ReplyDelete
  9. Ok, this is how I managed to do this in 2016 ->
    1. install vimFX
    2. start using it :) it is awesome !!!!
    3. choose option "stop websites from stealing focus on user input
    4. Be blessed.

    ReplyDelete

Hi spammers! No need to waste your time here; comments are heavily moderated, so if you like, you can save us both a little time and just move on to the next site. :-)

For everyone else: Thanks for visiting! Your comments are more than welcome!