<?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; javascript</title>
	<atom:link href="http://cmorrell.com/tag/javascript/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>
		<item>
		<title>2 Simple Twitter UI Tweaks</title>
		<link>http://cmorrell.com/microblogging/2-simple-twitter-ui-tweaks-7</link>
		<comments>http://cmorrell.com/microblogging/2-simple-twitter-ui-tweaks-7#comments</comments>
		<pubDate>Mon, 28 Jul 2008 21:40:33 +0000</pubDate>
		<dc:creator>Chris M.</dc:creator>
				<category><![CDATA[Microblogging]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[greasemonkey]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://cmorrell.com/?p=7</guid>
		<description><![CDATA[Update: it was bugging me, so I went ahead and wrote the greasemonkey script.  It highlights replies in yellow, and the last message you read in green.  Here&#8217;s the Userscripts.org page. I just recently started using Twitter, and so far &#8230; <a href="http://cmorrell.com/microblogging/2-simple-twitter-ui-tweaks-7">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><em><strong>Update: it was bugging me, so I went ahead and wrote the greasemonkey script.  It highlights replies in yellow, and the last message you read in green.  <a title="Chris Morrell's TwitterTweaks userscript" href="http://userscripts.org/scripts/show/30733" target="_blank">Here&#8217;s the Userscripts.org page</a>.</strong></em></p>
<p>I just recently started using Twitter, and so far I like it a lot more than I ever thought I would.  This is a problem, though, because I now use it enough to be very frustrated with its faults.  Obviously their scaling problems are an issue that many, many people have talked about, and there are a number of core changes that might make the service better (location-based tweets, additional tweet &#8220;types,&#8221; etc).  But I&#8217;m not talking about anything so grandiose—these are just two simple changes to the UI that would make me very happy.</p>
<h3>1. Show Where I Left Off</h3>
<p>Every time I refresh my twitter home page, I have to scroll down and find where I left off.  Wouldn&#8217;t it be nice if twitter remembered that for you and added a nice little separator in your timeline?  It&#8217;s a very simple interface tweak, but could save you a few seconds each time you check twitter.</p>
<h3>2. Highlight Replies</h3>
<p>Often, even after I&#8217;ve found the last tweet I&#8217;ve seen, I don&#8217;t have time to read through everything posted since then, so I skim.  Normally I&#8217;m looking for conversations that I&#8217;m either watching or a part of, or I&#8217;m looking for replies to my tweets.  Now conversations/threading is an issue that goes into the &#8220;bigger problem&#8221; category (an issue that <a title="Chris' Plurks" href="http://www.plurk.com/user/inxilpro">Plurk</a> has solved nicely, by the way), but showing replies should be easy as pie.  If a tweet has <a href="http://www.twitter.com/inxilpro">@inxilpro</a> in it, just highlight it or use some icon to differentiate it.</p>
<h3>Solution: Greasemonkey?</h3>
<p>I realize that both of these UI changes could be solved with a little Greasemonkey script.  In fact, I plan on writing such a script when I have a spare moment (hah!).  I also realize that both these problems are solved by a number of Twitter clients out there, so maybe I&#8217;ll give Twitterific another chance.</p>
<div class="su-linkbox" id="post-7-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/microblogging/2-simple-twitter-ui-tweaks-7&quot;&gt;2 Simple Twitter UI Tweaks&lt;/a&gt;" onclick="javascript:this.select()" readonly="readonly" style="width: 100%;" /></div></div>]]></content:encoded>
			<wfw:commentRss>http://cmorrell.com/microblogging/2-simple-twitter-ui-tweaks-7/feed</wfw:commentRss>
		<slash:comments>0</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 17/33 queries in 0.562 seconds using disk: basic
Object Caching 491/562 objects using disk: basic
Content Delivery Network via cdn1.cmorrell.com

Served from: cmorrell.com @ 2012-02-09 16:19:14 -->
