Neil's Place

April 23, 2009

Focus How To

Filed under: Mozilla — enndeakin @ 10:42 pm

In the new land of focus, there is a single focus manager service which handles focus. It keeps track of the topmost top-level window (called the active window), and the child frame window where the focus currently is located. For instance, if something within a particular tab is focused, then the active window is the chrome browser window containing it and the child frame window is the DOM ‘window’ loaded within the tab. If the address field is focused however, the child frame window is the chrome window itself. In this latter case, both the active window and the child frame window have the same value.

The window itself (nsGlobalWindow.cpp in the Mozilla source) stores the currently focused element within the window. This is stored for every window, whether a toplevel chrome window, a child window or a frame. You can retrieve this element from script using a document’s activeElement property. If that element is within the current child frame that the focus manager points to, then that element will be the one where key events will be dispatched to, it will generally have a focus ring around it, and so forth. Only one such element in the entire application will be in such a state at a time. It’s important to note that the current focused element is still maintained whether the window is actually focused or not. That way, when a window or tab is brought back to the front, the same element that used to be focused still will be.

There are four ways in which an element can be focused:

  1. The user clicks the element with the mouse.
  2. The user presses the tab key.
  3. The user presses the access key associated with the element, if any.
  4. The element’s focus() method is called.

The latter method is the way you would focus an element with a script. Whatever means is used, all calls are routed through a single function within the focus manager. This ensures that focus behaviour remains consistent whatever method is used.

The old way of doing focus had a myriad of methods, all undocumented and confusing. Here is a guide to what the new way will be in the future:

  • To retrieve the focused element, use the ‘activeElement’ property of the document. This will be set if the window is focused or not.
  • To modify the focused element, call the focus() method. This will make this element focused in its window or tab, but will not bring the window or tab to the front.
  • To switch to another window, call the window’s focus() method.
  • To clear the focus from an element, call its blur() method, or just focus something else.
  • From extensions and chrome code, in all other cases, call the focus manager. This service is available using the contract id ‘;1’ and the nsIFocusManager interface.

All other methods are deprecated or have been removed and should not be used. Specifically, this means the command dispatcher, docshells or any of the other shells, the event state manager, the focus controller, and well as some internal content node methods.



  1. I’ve been following the focus refactoring bug, it wasn’t an easy labour, congratulations 🙂

    Comment by PAStheLoD — April 24, 2009 @ 12:53 am

  2. What is specific major version of Firefox (3.6?) where this refactoring is expected to be actually implemented?

    Comment by MT — April 24, 2009 @ 9:28 am

  3. Neil: one problem we run into on the UI side is being able to determine if an object was focused by the keyboard or by the mouse. Will we be able to do that in this re-write? The practical application is that we would like to only draw focus-rectangles when the keyboard is being used, as opposed to drawing them on focus() like we do now.

    Comment by Mike Beltzner — April 24, 2009 @ 3:13 pm

    • Mike, see bug 418521 for this

      Comment by enndeakin — April 24, 2009 @ 3:24 pm

  4. Thanks. Congratulations.

    Comment by Gary Johnson — April 27, 2009 @ 5:37 pm

  5. If the browser search box is active, and is highlighting a result for the search, what is focussed? The search box or the highlighted phrase?

    Comment by holochair — August 8, 2009 @ 10:50 pm

  6. […] – UI geeks will note that Firefox has had a few issues regarding focusing elements. Thanks to some refactoring it’s vastly simplified and […]

    Pingback by Things You’ll Love About Firefox 3.6 | Robert Accettura's Fun With Wordage — January 20, 2010 @ 2:27 am

  7. […] Новая модель фокусировки активного ввода на элементы интерфейса (по умолчанию фокус на элемент определяется через указание свойства документа activeElement, при изменении фокуса отдельного элемента через focus() окно или таб теперь не всплывают принудительно на передний слой, для изменения фокуса на новое окно нужно вызвать метод focus() объекта window); […]

    Pingback by Релиз Firefox 3.6 « PHP Portal — January 23, 2010 @ 11:14 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: