OK, this has bugged me for a long time. Many times I’ll come to a site which helpfully auto-focuses on the input element I’m most likely to use (either the username field of a login form, or the query field of a search form). But if the script to make this happen is slow, I’ll often find myself typing over my already half-written search query, or even worse, typing my password (in plain text) into the user name field.
I’ve come up with a few possible solutions. To start, you could just skip the auto-focus code all together and let your users manually select the input they want to type in. This can be fine, but in my opinion, what separates good UI from great UI is when the system seems to know what you want and is one step ahead of you. So how can we avoid the problem while still providing the functionality?
Commenter Joe points out that all three of these techniques fail if you use your browser’s autocomplete to pre-populate the field, but want the web page to focus on the field so you can either hit return or enter a custom username. To handle this scenario I’ve come up with a new recommendation based on focus rather than content:
<script type="text/javascript">
$(document).ready(function() {
var el = $('#query')[0];
if (true !== el.hadFocus) {
el.focus();
}
});
</script>
<input type="text" onfocus="this.hadFocus = true;" id="query" />
So now, instead of checking to see if there’s any content in the “query” field, we’re checking to see if the user has focused on it. If they haven’t, it’s safe to change the focus there. If they have, you know they’re ahead of your code, and you can just get out of the way.
You might want to expand this to check if any field has been focused on. That way if someone starts typing in your search box, you don’t accidentally send them to the username field. But in general, something like the code above should be good in most situations.
Let me know if you see any potential problems.
Here are the approaches I’ve toyed with.
<input id="username" name="username" type="text" />
<script type="text/javascript">
<![CDATA[
if (document.getElementById)
document.getElementById('username').focus();
]]>
</script>
In simple situations (where the web app uses little to no other javascript), this is the option I go with. But it assumes a modern browser is available. And while you could use a Javascript framework to support more browsers, then you’d have the associated lag from loading the framework, which could reintroduce the original problem. That leads me to option 2…
window.addEvent('domready', function() {
var field = $('username');
if (field.value.length == 0) field.focus();
});
In this scenario, you run your auto-focus code once your framework (in this case mootools) and the DOM have loaded, but you check to see if the field has been typed in first. That way, if I type faster than your CDN loads, the code just doesn’t run. If you’re using a javascript framework, this is probably the best option.
There’s one other approach that I’ve always thought was interesting, but really doesn’t work in the end. I’m just putting it here for the fun of it…
First, output a disabled form element via javascript:
document.write('<input id="username" disabled="disabled" name="username" type="text" />');
Then, output an enabled element in noscript tags (just in case):
<noscript> <input type="text" name="username" id="username" /> </noscript>
And finally, run your code on DOM Ready:
window.addEvent('domready', function() {
var field = $('username');
field.disabled = false;
field.focus();
});
This way the input is disabled until your code loads. Though technically that method would work, it’s more likely to frustrate your power-users than enhance their user experience.
Have other people experienced this frustration? If you’re a web developer, are there other ways you have dealt with it? Leave a reply, or reply to @inxilpro on twitter.
Additional comments powered by BackType
You can leave a response, or trackback from your own site. Follow any responses to this entry through the RSS 2.0 feed.
Very usefull tutorial, brother!
Great idea except for one thing. When I go to a site where the homepage is mainly just a login page (like Facebook) and the browser auto-fills the username and password, I’d still like the username field auto-focused because I either just want to hit enter to submit the form and log in or I want to type a different username in because someone else is using my computer and wants to log in.
Your idea is great for other types of forms, though.
Good point… I suppose you could check focus instead of content. Add an onfocus event on the username and password field that sets a “hadFocus” boolean, and then check that. That way if the fields are pre-populated but never had focus you can make the same assumption. Maybe I’ll put together some sample code tomorrow.