<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Chris Morrell &#187; frustrations</title>
	<atom:link href="http://cmorrell.com/tag/frustrations/feed" rel="self" type="application/rss+xml" />
	<link>http://cmorrell.com</link>
	<description>The personal home page of Chris Morrell</description>
	<lastBuildDate>Thu, 02 Feb 2012 16:34:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Auto-Focus Input</title>
		<link>http://cmorrell.com/misc/auto-focus-input-45</link>
		<comments>http://cmorrell.com/misc/auto-focus-input-45#comments</comments>
		<pubDate>Thu, 21 Aug 2008 21:20:13 +0000</pubDate>
		<dc:creator>Chris M.</dc:creator>
				<category><![CDATA[frustrations]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cmorrell.com/?p=45</guid>
		<description><![CDATA[OK, this has bugged me for a long time.  Many times I&#8217;ll come to a site which helpfully auto-focuses on the input element I&#8217;m most likely to use (either the username field of a login form, or the query field &#8230; <a href="http://cmorrell.com/misc/auto-focus-input-45">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>OK, this has bugged me for a long time.  Many times I&#8217;ll come to a site which helpfully auto-focuses on the input element I&#8217;m most likely to use (either the <em>username</em> field of a login form, or the <em>query</em> field of a search form).  But if the script to make this happen is slow, I&#8217;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.</p>
<p>I&#8217;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?</p>
<h3>Update: Latest Technique</h3>
<p>Commenter Joe points out that all three of these techniques fail if you use your browser&#8217;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&#8217;ve come up with a new recommendation based on focus rather than content:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function() {
	var el = $('#query')[0];
	if (true !== el.hadFocus) {
		el.focus();
	}
});
&lt;/script&gt;
&lt;input type=&quot;text&quot; onfocus=&quot;this.hadFocus = true;&quot; id=&quot;query&quot; /&gt;
</pre>
<p>So now, instead of checking to see if there&#8217;s any content in the &#8220;query&#8221; field, we&#8217;re checking to see if the user has focused on it.  If they haven&#8217;t, it&#8217;s safe to change the focus there.  If they have, you know they&#8217;re ahead of your code, and you can just get out of the way.</p>
<p>You might want to expand this to check if <strong>any</strong> field has been focused on.  That way if someone starts typing in your search box, you don&#8217;t accidentally send them to the username field.  But in general, something like the code above should be good in most situations.</p>
<p>Let me know if you see any potential problems.<br />
<span id="more-45"></span></p>
<p>Here are the approaches I&#8217;ve toyed with.</p>
<h3>Option 1: Inline JavaScript</h3>
<pre class="brush: xml; title: ; notranslate">
&lt;input id=&quot;username&quot; name=&quot;username&quot; type=&quot;text&quot; /&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
&lt;![CDATA[
if (document.getElementById)
     document.getElementById('username').focus();
]]&gt;
&lt;/script&gt;
</pre>
<p>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&#8217;d have the associated lag from loading the framework, which could reintroduce the original problem.  That leads me to option 2&#8230;</p>
<h3>Option 2: Check Before Focus</h3>
<pre class="brush: jscript; title: ; notranslate">
window.addEvent('domready', function() {
	var field = $('username');
	if (field.value.length == 0) field.focus();
});
</pre>
<p>In this scenario, you run your auto-focus code once your framework (in this case <a href="http://www.mootools.net/" target="_blank">mootools</a>) 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 <a href="http://developer.yahoo.com/performance/rules.html#cdn" target="_blank">CDN</a> loads, the code just doesn&#8217;t run.  If you&#8217;re using a javascript framework, this is probably the best option.</p>
<p>There&#8217;s one other approach that I&#8217;ve always thought was interesting, but really doesn&#8217;t work in the end.  I&#8217;m just putting it here for the fun of it&#8230;</p>
<h3>Option 3: Make Them Wait</h3>
<p>First, output a <strong>disabled</strong> form element via javascript:</p>
<pre class="brush: jscript; title: ; notranslate">
document.write('&lt;input id=&quot;username&quot; disabled=&quot;disabled&quot; name=&quot;username&quot; type=&quot;text&quot; /&gt;');
</pre>
<p>Then, output an enabled element in noscript tags (just in case):</p>
<pre class="brush: xml; title: ; notranslate">
&lt;noscript&gt;
&lt;input type=&quot;text&quot; name=&quot;username&quot; id=&quot;username&quot; /&gt;
&lt;/noscript&gt;
</pre>
<p>And finally, run your code on DOM Ready:</p>
<pre class="brush: jscript; title: ; notranslate">
window.addEvent('domready', function() {
	var field = $('username');
	field.disabled = false;
	field.focus();
});
</pre>
<p>This way the input is disabled until your code loads.  Though technically that method would work, it&#8217;s more likely to frustrate your power-users than enhance their user experience.</p>
<p><strong>Have other people experienced this frustration? If you&#8217;re a web developer, are there other ways you have dealt with it?</strong> Leave a reply, or reply to <a href="http://www.twitter.com/inxilpro">@inxilpro</a> on twitter.</p>
<div class="su-linkbox" id="post-45-linkbox"><div class="su-linkbox-label">Link to this post!</div><div class="su-linkbox-field"><input type="text" value="&lt;a href=&quot;http://cmorrell.com/misc/auto-focus-input-45&quot;&gt;Auto-Focus Input&lt;/a&gt;" onclick="javascript:this.select()" readonly="readonly" style="width: 100%;" /></div></div>]]></content:encoded>
			<wfw:commentRss>http://cmorrell.com/misc/auto-focus-input-45/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 4/34 queries in 0.486 seconds using disk: basic
Object Caching 383/447 objects using disk: basic
Content Delivery Network via cdn1.cmorrell.com

Served from: cmorrell.com @ 2012-02-05 03:44:42 -->
