2008-01-27

Javascript Input Focus Annoyance

Many web sites have forms that move the keyboard cursor into an input field. For example, view your favourite search engine page and note that the cursor is in the search field. Enter a query, press RETURN, wait for the results, move the cursor out of the search field by hitting the TAB key, click on a link (don't open a new tab or window) then return to the search page. You should find that the cursor is back in the search field. If you usually just use the keyboard instead of the mouse, you have to hit TAB to move the cursor out of that field before you can scroll the page up or down. Some web sites have a search field in every page, so it is even more annoying to hit the TAB key in every page. (Why do I even persist in using these web sites?)

Web pages that display this behaviour usually use Javascript's focus() function. If you view the source code of such pages, you should see something like this: document. … .focus().

There's several solutions to this annoyance in Firefox.

The most general method is to disable Javascript by unchecking the Tools / Options / Content / Enable Javascript option, rather like using a sledgehammer to kill an ant.

If you just want to stop web pages from using the focus() method for the text fields (the INPUT tag), you can modify Firefox's security policy by editing your user.js preferences file:

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

Note: The policy named default is applied for all sites.

If you only want to stop certain sites from using the focus() method, create a new policy and specify when it should be applied, for instance:

user_pref("capability.policy.policynames", "noinputfocus");
user_pref("capability.policy.noinputfocus.sites", "<site list>");
user_pref("capability.policy.noinputfocus.HTMLInputElement.focus", "noAccess");

In this example, the noinputfocus policy is applied to the list of sites specified in noinputfocus.sites property.

28-Jan-2008: You have to restart Firefox before your policy is applied.

2 comments:

  1. to disable input focus by default and make some exceptions use this:

    user_pref("capability.policy.policynames", "focus_ok");
    user_pref("capability.policy.default.HTMLInputElement.focus", "noAccess");
    user_pref("capability.policy.focus_ok.sites", "http://dict.leo.org");
    user_pref("capability.policy.focus_ok.HTMLInputElement.focus", "allAccess");

    ReplyDelete
  2. I've tried both these methods and I can't seem to get the sites part to work. When I put in user.js FF moves it to prefs.js and removes one of the lines. Here is what it looks like from my user.js. It's on Vista with FF 1.9.11
    user_pref("capability.policy.default.HTMLInputElement.focus", "noAccess");
    user_pref("capability.policy.focus_ok.HTMLInputElement.focus", "allAccess");
    user_pref("capability.policy.focus_ok.sites", "http://www.google.com");

    ReplyDelete