Dear Gmail, please stop stealing my focus

I’m as much of a Gmail junkie as anybody, to the point where I rarely bother running mail servers anymore but instead opt for using Gmail via Google Apps.

Not that I think Gmail is perfect, just that all the webmail clients I’d used before gmail slow and annoying to the point of being practically useless.

I do have one annoyance with Gmail though and that is that frequently when I go to login, the focus is removed from the Username field and placed on the Password field halfway through me trying my user name. Like this:

gmail focus stealing

It doesn’t happen all the time and I haven’t looked into the exact circumstances (connection speeds, browser, plugins, etc) under which it happens but it’s definitely annoying. In terms of how the page is coded, what happens is that I start typing my username as soon as the page comes up, then the window.onload() event fires, then the focus is moved to the password field because there is already something in the username field. The code is there to make it quicker for people who have the username already populated by their browser. In my case though, with my fingers on autopilot as soon as the page loads this intended convenience turns into an inconvenient annoyance.

In my last post about intentionality & adverse effects I wrote about the dangers of making unrefined guesses about what the user wants. I think that is going on here - for me at least.

I know that a similar trick on the Google homepage has been called the Billion dollar line of JavaScript but in the case of this one, I don’t see why Google haven’t done something about it (assuming that I’m not the only person in the Googleverse getting annoyed by this one.)

The reason I don’t see why Google haven’t done something is that the fix looks pretty simple. I know it is a bit presumptuous of me to think that I can look at this and solve the problem in a matter of minutes so read the following suggestion with that disclaimer in mind.

This is definitely not how I would recommend they code it but instead just demonstrates very roughly what could be done to the existing code (my additions are indicated by the comments):

// global flag allowing focus switch or not
// I wouldn't normally use a global variable, this is only to 
// demonstrate the potential change in the simplest way
var gaia_allowFocusSwitch = true;

// Set the global flag to disallow if the user types into the Username field
// This would need to be attached before the onload event fires so the exact
// placement of it, and any capability or element checks would also need to be done here
document.getElementById("gaia_loginform").Email.onkeypress = function() {
    gaia_allowFocusSwitch = false;

function gaia_setFocus() {
    var f = null; 
    if (document.getElementById) { 
        f = document.getElementById("gaia_loginform");
    } else if (window.gaia_loginform) { 
        f = window.gaia_loginform;
    if (f) {
        if (f.Email && (f.Email.value == null || f.Email.value == "")) {
        // add a check on the global flag to the password focus switch
        } else if (f.Passwd && gaia_allowFocusSwitch) {
            f.Email.onfocus = function() {
                gaia_allowFocusSwitch = false;

If the user has started typing into the Username field, then don’t move the focus to the Password field.

Could it be that simple? Given the expertise that Google has in these things there’s a decent chance that there is a very good reason why it isn’t already setup to do something along those lines.

I’m certainly happy to hear from anyone who knows.

CoffeeScript in Action

CoffeeScript in Action book cover

I'm the author. Get it from Manning.