27 January 2008

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.