<?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>Tech Blog &#187; User experience</title>
	<atom:link href="http://assanka.net/content/tech/tag/user-experience/feed/" rel="self" type="application/rss+xml" />
	<link>http://assanka.net/content/tech</link>
	<description>Just another Arb-assk2009003.turmeric.assanka.com Blogs weblog</description>
	<lastBuildDate>Mon, 29 Aug 2011 19:00:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tips for better fragment navigation</title>
		<link>http://assanka.net/content/tech/2011/02/16/tips-for-better-fragment-navigation/</link>
		<comments>http://assanka.net/content/tech/2011/02/16/tips-for-better-fragment-navigation/#comments</comments>
		<pubDate>Wed, 16 Feb 2011 10:05:07 +0000</pubDate>
		<dc:creator>Andrew Betts</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[fragments]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[keyboard]]></category>
		<category><![CDATA[Scrolling]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[User experience]]></category>

		<guid isPermaLink="false">http://assanka.net/content/tech/?p=170</guid>
		<description><![CDATA[Fragment navigation is becoming more and more popular.  Facebook practically runs their entire site on it, twitter search uses it, and Google recently defined a standard for making it crawl-able.  JavaScript framework evangelists have rushed to produce plug-ins for their favourite tool-kit to make fragment navigation easier to implement.  Here I&#8217;ll discuss [...]]]></description>
			<content:encoded><![CDATA[<p>Fragment navigation is becoming more and more popular.  Facebook practically runs their entire site on it, twitter search uses it, and Google recently defined a <a href="http://code.google.com/web/ajaxcrawling/docs/getting-started.html">standard for making it crawl-able</a>.  JavaScript framework evangelists have rushed to produce plug-ins for their favourite tool-kit to make fragment navigation easier to implement.  Here I&#8217;ll discuss some of the issues we&#8217;ve dealt with as we started to use fragment navigation more often.</p>
<p><img src="http://assanka.net/content/tech/files/2010/08/new-1.png" alt="" title="Facebook example of fragment navigation" width="590" height="72" class="alignnone size-full wp-image-178" /></p>
<p>The details of how fragment navigation (also hash navigation or hash fragment navigation) works have been <a href="http://www.novatek.com.au/news/fragment-navigation">covered</a> <a href="http://yensdesign.com/2008/11/creating-ajax-websites-based-on-anchor-navigation/">extensively</a> by <a href="http://msdn.microsoft.com/en-us/library/cc891506(VS.85).aspx">others</a>, and have been abstracted into various frameworks and toolkits.  A number are available for jQuery, such as:</p>
<ul>
<li><a href="http://plugins.jquery.com/project/history">History</a></li>
<li><a>BBQ</a> (<a href="http://mattfrear.com/2010/03/20/enabling-browser-back-button-on-cascading-dropdowns-with-jquery-bbq-plugin/">tutorial</a>)</li>
<li><a href="http://www.asual.com/jquery/address/">Address</a></li>
</ul>
<p>However, fragment navigation comes with a new set of challenges that we found ourselves having to address, so these will be the focus of this post.</p>
<h2>Full page alternatives</h2>
<p>Rather than changing all links to the form <code>&lt;a href="#!fragment/path/for/ajax"&gt;Link&lt;/a&gt;</code> and then detecting and acting upon fragment changes in javascript, we wanted to ensure that our links worked when they were copied and pasted into a new browser window, shared by email, or used with javascript disabled.  This meant making them into real URL links, and then progressively enhancing using a Javascript onclick handler to cancel the normal navigation action:</p>
<pre class="brush: xml;">
&lt;a href=&quot;/path/works/as/fragment/or/normal/page&quot; class=&quot;fragnav&quot;&gt;Link&lt;/a&gt;
</pre>
<p>And some jQuery to hook the onclick handler on these fragment-navigation-compatible links, and convert them so that rather than navigating to the specified href, the browser changes it&#8217;s hash instead:</p>
<pre class="brush: jscript;">
$(&quot;a.fragnav&quot;).click(function() {
  var href = this.href.replace(/^https?:\/\/[a-z0-9\.]+\/(.*\#\!)?/, '');
  location.hash = this.href;
  return false;
});
</pre>
<p>Now you can either click the link with javascript enabled and get a dynamic AJAX behaviour, or right click then &#8216;Open in new window&#8217; (or click with javascript disabled) to get the full URL of the link to load normally.</p>
<h2>Caching</h2>
<p>When your user presses &#8216;back&#8217;, you can detect the fragment change and reload appropriate content &#8211; great.  But this is not as good as what you get from browsers&#8217; normal back button behaviour.  Normally, the previous page is cached, so it reappears almost instantly.  In the AJAX version, unless your fragment is just switching between different versions of content that are all preloaded, you may fire an AJAX request to load the content again.</p>
<p>It shouldn&#8217;t be necessary to reimplement caching yourself, since AJAX requests are subject to the same browser caching as normal page loads, but it&#8217;s easy to forget that you should include appropriate cache headers with your AJAX responses.  There are four HTTP headers that generally modify a browser&#8217;s caching behaviour:</p>
<ul>
<li>Cache-Control (<a href='http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9'>Docs</a>)</li>
<li>Expires (<a href='http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21'>Docs</a>)</li>
<li>ETag (<a href='http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.19'>Docs</a>)</li>
<li>Last-Modified (<a href='http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.29'>Docs</a>)</li>
</ul>
<p>Cache-Control and Expires are cache directive headers, telling the browser whether and for how long it may cache the resource.  Typically we set only Cache-Control, as it is more powerful and flexible, and Expires is generally unnecessary.</p>
<p>ETag and Last Modified are validators.  These allow the browser to make a conditional request to the server to find out if the resource has changed, and allow the server to respond with a simple 304 Not Modified if it hasn&#8217;t.  It&#8217;s often the case that your web server will add these automatically, and we prefer to avoid them entirely, relying on a single Cache-Control header to determine browser behaviour.</p>
<pre class="brush: xml;">
Cache-Control: max-age:60, public
</pre>
<p>A nice trick for content that is personalised to an authenticated user is to include the <code>private</code> directive in your Cache-Control header, which allows the page to be cached, but only by the browser, not by any proxies along the way.</p>
<h2>Scroll position</h2>
<p>One of the things we found to be most difficult was managing scroll position.  Consider the default behaviour of a browser when navigating normally.  If you scroll down a page and click a link somewhere near the bottom, the browser loads the new page and displays it starting at the top.  However, if you then press the back button, it redisplays the last page you viewed and <strong>restores your scroll position for that page</strong>.  You may not have even noticed this, but if it stopped behaving this way you&#8217; get pretty annoyed soon enough!</p>
<p>So, if a user clicks one of your fragment links low down on your page, and the change you make to the page as a result would appear to the user to constitute a new page (obviously the point at which perception of &#8216;a new page&#8217; is triggered is subjective), consider scrolling the browser window back to the top.</p>
<p>But, you should not do this if the back button is used, because the browser will restore the user&#8217;s previous scroll position all by itself.  However, this becomes more complex if navigating forwards has considerably shortened the page content, and restoring the scroll position on back would require you to restore the content first in order for the necessary scroll offset to actually exist.  This is a bit confusing.  Here&#8217;s an illustration:</p>
<p><img src="http://assanka.net/content/tech/files/2011/01/ajaxnavgraphic1.png" alt="Illustration of problems with scroll position when using AJAX navigation" title="Illustration of problems with scroll position when using AJAX navigation" class="aligncenter size-full wp-image-197" /></p>
<p>So the solution we use is to remember the scroll position on every navigation action, and then restore it after repopulating the content if it looks like a backwards step.  You need to ensure you have got your caching rules right to support this otherwise the delay in refetching the content will make the browser reposition the scroll position twice &#8211; once by itself immediately, and again triggered by your JavaScript after the content has loaded.</p>
<h2>Loading pause</h2>
<p>Normally, the process of clicking a link and navigating to a new page involves the current page <strong>remaining on screen</strong> while the new page is being requested.  The browser only blanks it out when content starts to arrive for the new page.  The browser does provide some progress feedback immediately though, in the form of a wait mouse cursor or spinner in the browser chrome.</p>
<p>We aimed to replicate this experience for our AJAX-loaded views.  This means avoiding what seems like an obvious solution &#8211; empty the container when the link is clicked, and populate it when the AJAX response is received &#8211; because you&#8217;ll get a flash of blankness between the old content disappearing and new content replacing it.  Instead, the old content should remain, and the new content should simply replace it when the AJAX completes.</p>
<p>But this doesn&#8217;t provide progress feedback.  The risk is, the user will think nothing&#8217;s happened and will click the link again.  This tends to happen after about 2-3 seconds for most users, but AJAX calls normally don&#8217;t take anywhere near that long.  So we implemented a timeout that, after 250ms, would blank out the container, replacing the old content with a loading spinner.  If the AJAX completes within that time, it cancels the timer.</p>
<p>So, in most cases the user clicks a link and sees an almost immediate cut from old content to new with no flash of blankness.  In some cases they see the old content vanish and a loading spinner to reassure them that something is happening while we wait for the new content to come back.</p>
<p>If you regularly have edge cases where content takes more than a few seconds to load, you should consider moving on to a different kind of progress feedback, to avoid hitting that &#8220;it hasn&#8217;t worked&#8221; perception boundary.  The aim is to keep pushing that boundary back, keeping the user&#8217;s expectations in line with the performance that they&#8217;re getting from the site.  I like to think of this like a timeline:</p>
<p><img src="http://assanka.net/content/tech/files/2011/01/ajaxnavgraphic2.png" alt="Timeline of user perception when waiting for a navigation action to complete" title="Timeline of user perception when waiting for a navigation action to complete" width="515" height="169" class="aligncenter size-full wp-image-199" /></p>
<p>By providing better progress feedback, you can expand the &#8216;Expectation&#8217; phase and avoid the &#8216;Frustration&#8217; phase.  However, it&#8217;s also equally important that you don&#8217;t display a big piece of progress feedback for the entire operation to then complete in under 250ms &#8211; the feedback will be on screen for such a short period that the user will potentially be unsure about what happened and get anxious.  So be aware of the &#8216;instant&#8217; phase, when you should present no feedback at all.</p>
<h2>Inline Javascript</h2>
<p>Sometimes, it&#8217;s convenient to get both new HTML code and new Javascript when your user clicks a fragment link.  In our case, we wanted to set some Javascript globals in the response to allow javascript code already loaded on the page to better understand the content that had just been loaded.  Say the user has requested a page with a fragment that loads some search results.  We also want to tell the browser the search query that produced the results so that it can cache them, or populate the query into a search field that&#8217;s outside the fragment container, or something.</p>
<p>Normally, if you load content from a server using AJAX and append it to the DOM using innerHTML, any &lt;script&gt; sections in the HTML are not parsed by the browser&#8217;s JavaScript engine.  However, to make it do this is as simple as searching the returned source for &lt;script&gt; sections, and evaling them.  Make sure you properly filter and escape end user input before allowing it to be evaled though, otherwise you&#8217;re opening up your site to XSS attacks.</p>
<pre class="brush: jscript;">
$('#main script').each(function() { eval(this.text); });
</pre>
<h2>Command, Control and Shift modifier keys</h2>
<p>Power users like to open links in new tabs or windows.  In fact when faced with a list of links I often hold down Control and hit each link to turn to start preloading all the results into tabs which I can then review in turn without having to wait for each one to load.  To avoid annoying users, you need to ensure that whereever possible, control-click (or command-click) and shift-click (normally &#8216;new tab&#8217; and &#8216;new window&#8217; respectively) still work.</p>
<p>It&#8217;s easy to get lulled into a false sense of security by right clicking on your links and using the context menu to select &#8216;Open in new tab&#8217;.  This won&#8217;t run your onclick handler, so most likely will work if your link has a non-JavaScript href.  But using the keyboard CTRL key while clicking the link will fire the onclick event, and if you are cancelling the normal link navigation action by returning false, you&#8217;ll also be cancelling the new tab or new window request.  Ensuring that you don&#8217;t is quite straightforward (remember to include e.metaKey to detect the Command key on Macs):</p>
<pre class="brush: jscript;">
if (e.shiftKey || e.ctrlKey || e.metaKey) return true;
</pre>
<h2>Focus rectangles</h2>
<p>In most browsers, when you click a link, you get a small dotted rectangle around it.  This focus rectangle also appears if you tab through the actionable items on a page, to indicate which one would be followed if you pressed enter.  The problem is that if your link fires an AJAX action, and the link itself remains on screen after the action has completed, you&#8217;re left with a focus rectangle that you probably don&#8217;t want.</p>
<p>A bad way of dealing with this is to simply use CSS to set <code>outline:none</code> on all A tags.  This will certainly solve your problem, but kill all hope of keyboard navigation of your site.  Better is to study the way that this interaction happens normally, and then mimic it for your Ajax actions.  This is actually fairly easy to do generically:</p>
<pre class="brush: jscript;">
(function() {

  // Keep track of which link element last had the focus (if browser does not support document.activeElement)
  if (!document.activeElement) {
    $(&quot;a&quot;).live('focus', function() {
      document.activeElement = this;
    });
  }

  // When any AJAX operation completes, blur any focused link
  $.ajaxSetup({complete:function(xhr, textStatus) {
    if (document.activeElement &amp;&amp; document.activeElement.is('a')) document.activeElement.blur();
  }});
}());
</pre>
<p>Drop the above snippet into your javascript and you should find that focus rectangles still appear and stay visible while the ajax is running, but then vanish once it completes &#8211; perfect replica of the effect the user has learned to expect.  If your links don&#8217;t perform any AJAX, you&#8217;ll have to deal with those separately.</p>
<h2>Conclusion</h2>
<p>With all this to think about, you&#8217;d be forgiven for concluding that AJAX navigation simply isn&#8217;t worth the bother.  But bear with it &#8211; the benefits of being able to trivilally maintain state and load small fragments of content really makes a difference, both to the quality of the user experience and the load on your servers.</p>
]]></content:encoded>
			<wfw:commentRss>http://assanka.net/content/tech/2011/02/16/tips-for-better-fragment-navigation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Faceted search: choosing good facet suggestions</title>
		<link>http://assanka.net/content/tech/2010/11/16/faceted-search-choosing-good-facet-suggestions/</link>
		<comments>http://assanka.net/content/tech/2010/11/16/faceted-search-choosing-good-facet-suggestions/#comments</comments>
		<pubDate>Tue, 16 Nov 2010 10:05:50 +0000</pubDate>
		<dc:creator>Andrew Betts</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[faceting]]></category>
		<category><![CDATA[filters]]></category>
		<category><![CDATA[keywords]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[strategy]]></category>
		<category><![CDATA[suggestions]]></category>
		<category><![CDATA[User experience]]></category>
		<category><![CDATA[xapian]]></category>

		<guid isPermaLink="false">http://assanka.net/content/tech/?p=175</guid>
		<description><![CDATA[Faceted search is everywhere, making our online shopping experience easier, organising our photos, searching our DVD collection.  By showing filters in categories, you can allow users to search the way they want to, rather than in a prescribed category hierarchy of your choice.  We&#8217;ve recently used faceted search in a number of applications [...]]]></description>
			<content:encoded><![CDATA[<p>Faceted search is everywhere, making our online shopping experience easier, organising our photos, searching our DVD collection.  By showing filters in categories, you can allow users to search the way they want to, rather than in a prescribed category hierarchy of your choice.  We&#8217;ve recently used faceted search in a number of applications and found that one area is a particular challenge: choosing the suggested values under each category.</p>
<p><img src="http://assanka.net/content/tech/files/2011/01/moodys.png" alt="FT Tilt&#39;s faceting uses a blend of strategies" title="FT Tilt&#39;s faceting uses a blend of strategies" width="515" height="305" class="aligncenter size-full wp-image-217" /><br />
<cite>The site Assanka recently launched for <a href="http://tilt.ft.com">FT Tilt</a> uses a variety of faceting strategies to give the most appropriate suggestions in each category</cite></p>
<p>Most implementations of faceting always show the same suggestions in any given category, but may change the filters available based on the search the user has done so far.  For example, if you&#8217;re searching a computer supplies retailer for a new hard disk, most of the products matching your search will have a &#8216;capacity&#8217; property, so the retailer will probably offer you a capacity filter with all available capacities listed.  It probably won&#8217;t offer you a &#8216;pages per minute&#8217; filter, because resolution is not relevant to hard disks, and your existing search keywords have probably eliminated any product (like a monitor) for which a resolution choice would make sense.</p>
<p>So it&#8217;s pretty easy to choose which filters to display.  The more difficult problem is choosing which facets to display within each filter.</p>
<p>Sometimes it&#8217;s impractical to show all possible facets in a filter.  eBay, for example, allows you to restrict your search by seller, and of course there are millions of those.  So there are a number of possible strategies:</p>
<ol>
<li>Display every option available</li>
<li>Display just a few hard coded options, such as aggregate options designed to always match something (eg eBay&#8217;s &#8216;Top rated sellers&#8217; option), or editorially chosen &#8216;top picks&#8217;.  Either way, the point is that the suggestions are a subset of the full range available, and are not sensitive to the context created by the user&#8217;s search query</li>
<li>Use the list of options as per (1) or (2), but hide any that, if selected, would produce no results.  This refinement makes the suggestions context-sensitive in a very rudimentary way, in that they are at least reacting to the current state of the user&#8217;s search, but still do not surface any long-tail options when they become more relevant.</li>
<li>Generate options based on running the query the user has put together so far, including any facets selected, and analysing the resultset for the top facet refinements in each filter category.</li>
<li>As per (4), but for each filter category, run the search excluding any options already selected in that category.</li>
<li>As per (4) or (5), but where the values are all numeric, determine the facets not from the frequency of occurrence of specific values, but by analysing the distribution of values within the resultset and constructing boundaries that provide a sensible number of divisions with approximately the same number of results in each division.</li>
</ol>
<p>There are arguments for and against all of these, depending on the distribution of the metadata within your document index.</p>
<p>An e-commerce site will typically have a set of properties on a product, where each property has one value, and won&#8217;t use all the available property names.  The values available for each property will also typically be a range set, and will efficiently cover the whole available range.  For example a hard disk will have a &#8216;capacity&#8217; property, where the options might include 200GB, 300GB, 500GB and 1TB.  Only one of these can apply to any given hard disk.  A hard disk, as previously discussed, will also not make use of other available properties, such as resolution, that might apply to other types of product, such as monitors.  A property like &#8216;pages per minute&#8217; is really only going to be used on a very small subset of your product catalogue (only printers).  &#8216;Capacity&#8217; might get a bit more limelight as it applies to USB sticks, RAM and storage appliances as well as hard disks (though consider that the option values required in these types of products might be in a different range), and some properties like &#8216;manufacturer&#8217; would apply to virtually the entire product catalogue.</p>
<p>Sometimes, a product might have more than one applicable value in the same category.  Take an example category &#8220;Special offers available&#8221;.  A single product might qualify for &#8220;Buy one get one free&#8221; as well as &#8220;Free delivery&#8221;.   This kind of thing actually happens more in the non-retail world, and a better example would be a film library, where a film may have more than one actor, more than one screenwriter, more than one content advisory.  Where this is the case, filtering on one actor does not necessarily mean you&#8217;ve excluded all the others from the resultset.</p>
<p>The distribution is also relevant.  In the &#8216;capacity&#8217; example, we could expand the number of values to include more granularity below 50GB to allow for solid state devices, and above 1TB to allow for storage appliances, but in any given search, results will tend to form an unequal distribution with a peak, or multiple peaks, in particular capacities.  Across a range of hard disks, at time of writing 100-500GB would likely be the most popular value.  On the other hand take a category like &#8216;Actor&#8217; in the case of a film library, and the distribution looks a lot flatter.  An actor can only do so many films, and there are a lot of actors, so there isn&#8217;t a strong head to this distribution &#8211; it&#8217;s all tail.</p>
<p>Looking at each filter category in turn, the logic for deciding how to choose suggested values therefore comes down to a number of questions about how the category is used to classify your content:</p>
<ol>
<li>Are there few enough values that you could display them all together?</li>
<li>Do the values form a continuous range (like capacity) or are they discrete options (like actor)?</li>
<li>Is it possible for any single item of content to have more than one value from the same category?</li>
<li>Is there a &#8216;head&#8217; of a few values (few enough to display all of them together) which, combined, apply to a majority of your content?</li>
</ol>
<p>It&#8217;s important to stress that these are not decisions to take for your site as a whole &#8211; they need to be applied to each filter category individually.</p>
<p>There is one final consideration.  Is your faceting feature intended to help users narrow their search, change it, or broaden it?</p>
<p>Going back to the possible strategies for determining facets, displaying every option available works for small categories, and using hard coded options groups like &#8216;top 100 sellers&#8217; is basically a solution for displaying every option available by consolidating many options into one.  Doing this where necessary (and then also hiding any options that would result in no matches) gives you about the best solution you are likely to get without going context sensitive.</p>
<p><img src="http://assanka.net/content/tech/files/2011/01/Capture1.png" alt="Amazon BBFC ratings" title="Amazon BBFC ratings" width="210" height="68" class="aligncenter size-full wp-image-215" /><br />
<cite>The DVD search on <a href="http://www.amazon.co.uk">Amazon.co.uk</a> displays all possible options in the BBFC rating category, and hides any that don&#8217;t apply to the results.  In this case, the results found by the search only contain films rated PG, 15 and 12.</cite></p>
<p>It starts to get interesting when you have a lot of possible values, in a flattish distribution, and want to present specific, context sensitive options.  The sense of &#8216;context&#8217; depends on whether you want to help broaden or narrow the search.  </p>
<p>Take a search for the location &#8220;UK&#8221; and animal &#8220;Dog&#8221;, which will give you results referring to dogs in the UK.  One of the facet categories might be location, in which there is one option that is already a term in the search &#8211; UK.  Determining the facets to suggest in the location category by only looking at the results returned in the existing search would only yield locations that co-exist on items tagged with UK and dogs, so your filter refinements would be places like &#8220;Manchester&#8221;, &#8220;Birmingham&#8221;, &#8220;Liverpool&#8221;, &#8220;London&#8221;, &#8220;Cardiff&#8221;, &#8220;Edinburgh&#8221;.  This helps the searcher to refine their search.</p>
<p><img src="http://assanka.net/content/tech/files/2011/01/Capture.png" alt="Lovefilm&#39;s search facets offer refinement" title="Lovefilm&#39;s search facets offer refinement" width="423" height="171" class="aligncenter size-full wp-image-213" /><br />
<cite>The film search on <a href="http://www.lovefilm.com">Lovefilm</a> presents facets that narrow your search.  It also arranges the facets in a hierarchy, though this is an illusion</cite></p>
<p>However, using strategy 5, you run the search once on &#8220;UK Dogs&#8221; to produce the results to show the user, but you run it again on &#8220;Dogs&#8221;, excluding the term from the locations category, to generate location facets.  This time you get locations globally that co-exist with Dogs, and the suggestions for facets in any given category do not change if you choose to add a facet from that category to your search.  In this case the suggestions would be more likely to be &#8220;Paris&#8221;, &#8220;New York&#8221;, &#8220;France&#8221;, &#8220;Boston&#8221;, &#8220;London&#8221;, &#8220;United States&#8221;.  This helps to broaden the search by showing the user good suggestions for other locations that also have lots of dogs that they may not have considered.</p>
<p>Finally, where the values are numeric, it may be appropriate to produce dynamic facets as range boundaries, based on an analysis of the values that exist within the resultset.  You still follow one or other of the above strategies to get a resultset that either narrows or broadens the search, but then analyse the list of values and construct divisions that evenly partition the data into a small number of ranges.  Doing this with a search for say the product type &#8220;Hard disks&#8221; and the capacity &#8220;100-150GB&#8221;, the suggested capacity facets would subdivide the selected range, with narrow boundaries covering the most popular capacities, and wider ranges where there are fewer results.</p>
<p><img src="http://assanka.net/content/tech/files/2011/01/facet1.png" alt="Ebuyer&#39;s faceting of numeric categories is inconsistent" title="Ebuyer&#39;s faceting of numeric categories is inconsistent" width="391" height="188" class="aligncenter size-full wp-image-211" /><br />
<cite><a href="http://www.ebuyer.com">Ebuyer&#8217;s</a> product search contains lots of faceting of numeric categories, some of which are presented in dynamic ranges, and some are treated as standard terms</cite></p>
<p>For numeric categories like this, it&#8217;s also worth considering the sort order of the facet list.  For categories of non-numeric values and sometimes even for discrete numeric values (say &#8216;film speed&#8217; or &#8216;quantity per box&#8217;), it&#8217;s generally best to present them in decreasing order of popularity within the search context you&#8217;ve chosen.   For numeric categories where you&#8217;re constructing range facets (eg. &#8216;capacity&#8217;, &#8216;pixel density&#8217;, &#8216;brightness&#8217;), they should instead be presented in value order.</p>
<h2>Conclusion</h2>
<p>There are many ways of faceting search results.  Take some time to choose the one that suits your application best, and provides the best search experience for your users.  We use the open source search engine <a href="http://www.xapian.org">Xapian</a>, which is excellent at doing all the faceting described in this post, and I&#8217;d like to publicly acknowledge <a href='http://cnav.co.uk/'>Richard Boulton</a> for his excellent advice when we were designing faceting strategy for FT Tilt.</p>
]]></content:encoded>
			<wfw:commentRss>http://assanka.net/content/tech/2010/11/16/faceted-search-choosing-good-facet-suggestions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blocking events with blocker lists</title>
		<link>http://assanka.net/content/tech/2009/12/07/blocking-events-with-blocker-lists/</link>
		<comments>http://assanka.net/content/tech/2009/12/07/blocking-events-with-blocker-lists/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 14:42:35 +0000</pubDate>
		<dc:creator>Andrew Betts</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Scrolling]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[User experience]]></category>

		<guid isPermaLink="false">http://assanka.net/content/tech/?p=56</guid>
		<description><![CDATA[It&#8217;s often useful to be able to detect scroll events using the onscroll event handler in JavaScript.  For example, every time a user scrolls to nearly the bottom of the page, you load more content to create an &#8216;endless&#8217; page.  In my case, I have two DIVs set to overflow: auto, with chat [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s often useful to be able to detect scroll events using the onscroll event handler in JavaScript.  For example, every time a user scrolls to nearly the bottom of the page, you load more content to create an &#8216;endless&#8217; page.  In my case, I have two DIVs set to overflow: auto, with chat histories in each, where the chats have been taking place simultaneously.  I want to detect when the user scrolls one of the DIVs (either one) and then scroll the other one to keep the two in sync.  That is to say, we want messages that are currently vertically in the middle of DIV 1 to have been posted at around the same time as the messages at the same vertical viewport offset in DIV 2.</p>
<p>Try scrolling either of the panels in this example:</p>
<div class="iframe-wrapper">
  <iframe src="/content/tech/files/2009/12/testcase_blockerlist11.html" frameborder="0" style="height:220px;width:450px;">Please upgrade your browser</iframe>
</div>
<p>You should find it scrolls madly and unpredictably until you press stop.</p>
<p>The first problem is that onscroll is not simply fired when you finish scrolling.  It fires like a machine gun continuously while the mouse cursor is moving on the scroll handle.  The solution to this is a watchdog.  A watchdog is a timer that will execute action A if action B does not happen within X seconds.  So every time onscroll fires, we reset the timer, and if it gets to zero we know that the user has finished scrolling (or at least has paused for long enough for us to do something about it).</p>
<div class="iframe-wrapper">
  <iframe src="/content/tech/files/2009/12/testcase_blockerlist2.html" frameborder="0" style="height:220px;width:450px;">Please upgrade your browser</iframe>
</div>
<p>You&#8217;ll find that although it&#8217;s not quite as crazed, the panels do keep scrolling, one after the other.</p>
<p>The problem now is that when you scroll DIV 1, and this causes an automatic scroll of DIV 2, that automatic scroll ALSO triggers the onscroll event and so we then scroll DIV 1 again.  The solution is to have a variable that flags whether we are currently paying attention to scroll events, and turn it off when we don&#8217;t want to detect scrolling (ie just before the auto-scroll) and then on again after the scroll has completed (I&#8217;m using an animation library so the scroll takes around half a second to complete).  Try this (don&#8217;t click the button yet, just scroll the panels):</p>
<div class="iframe-wrapper">
  <iframe src="/content/tech/files/2009/12/testcase_blockerlist3.html" frameborder="0" style="height:220px;width:550px;">Please upgrade your browser</iframe>
</div>
<p>Success.  However, this approach is a bit short-sighted.</p>
<p>You also need to turn off scroll detection when adding or removing content from either DIV, because changing the height of the content within the element can also fire the onscroll event.  So if this is a chat window, and we&#8217;re adding and removing content in the DIVs, we have to disable scroll detection while we&#8217;re doing that and then turn it on again afterwards.</p>
<p>Sadly, our scroll detection flag is crude &#8211; it&#8217;s a sheer yes/no, and if it&#8217;s already set to no (say in order to execute a lengthy scrolling animation), and in the meantime we need to do something quick (say adding a line to one of the DIVs) then that quick operation is going to disable scroll detection (unnecessarily, as it&#8217;s already disabled) and then crucially enable it again before the scroll animation has finished.  Click the button in the example above to demonstrate this (and then scroll).  You should, intermittently, get the unwanted cascade effect you saw in example 2.</p>
<p>What we need is a list, not a flag.  Enter the blocker list &#8211; an object that collates tokens from each procedure in the script that is currently blocking scroll detection.  So if a procedure wants to disable scrolling for a period of time, it adds its token to the blocker list, and then removes its token when it&#8217;s done.  When a scroll event fires, we now only need to know whether there are any items in the blocker list, and then we can work out if it&#8217;s ok to process the event.</p>
<p>It&#8217;s also worth noting that there&#8217;s no need to actually count the number of items in the blocker list &#8211; it&#8217;s enough simply to know that there is at least one item in there.  And as a further optimisation, we don&#8217;t actually need to compute this when scroll events fire (frequently), because the value will only change when some function wants to add or remove its token from the list (less frequent).  So we can have enableScrollDetection and disableScrollDetection functions that deal with the blocker list, and which ultimately simply change the old scrolldetection flag to true or false.</p>
<div class="iframe-wrapper">
  <iframe src="/content/tech/files/2009/12/testcase_blockerlist4.html" frameborder="0" style="height:220px;width:550px;">Please upgrade your browser</iframe>
</div>
<p>There might be a neater way of achieving this, but this certainly works for me.  I&#8217;d welcome any comments or suggestions.  The sources for all these demos are available in the iframes above.</p>
]]></content:encoded>
			<wfw:commentRss>http://assanka.net/content/tech/2009/12/07/blocking-events-with-blocker-lists/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Disappearing text cursor in Firefox</title>
		<link>http://assanka.net/content/tech/2009/11/25/disappearing-text-cursor-in-firefox/</link>
		<comments>http://assanka.net/content/tech/2009/11/25/disappearing-text-cursor-in-firefox/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 13:03:15 +0000</pubDate>
		<dc:creator>Andrew Betts</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[User experience]]></category>

		<guid isPermaLink="false">http://assanka.net/content/tech/?p=43</guid>
		<description><![CDATA[Do you ever find that sometimes when you try and type into a textbox, there is no cursor there, but it still accepts your input and the text appears as if there is a cursor?  I came across this problem in Firefox 3 and searching online revealed only solutions to an earlier problem that [...]]]></description>
			<content:encoded><![CDATA[<p>Do you ever find that sometimes when you try and type into a textbox, there is no cursor there, but it still accepts your input and the text appears as if there is a cursor?  I came across this problem in Firefox 3 and searching online revealed only <a href="http://www.nestedelements.com/2008/02/26/firefoxs-disappearing-cursor/">solutions</a> <a href="http://www.webdeveloper.com/forum/showthread.php?t=150640">to</a> <a href="http://blog.tremend.ro/2007/01/22/mouse-cursor-disappears-in-firefox/">an</a> <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=167801">earlier</a> <a href="http://www.fleegix.org/articles/2007-02-14-mystery-of-the-disappearing-cursor-caret">problem</a> that affected Firefox 2, in which the overflow configuration of the INPUT&#8217;s containing element would affect whether the cursor appeared in the input or not.</p>
<p>But this was clearly not my problem, and various sources suggested that this had in any case been fixed for Firefox 3.  I discovered that my problem was rather simpler.  If you disable an element that has focus, and then re-enable it, you don&#8217;t get the cursor back.</p>
<p>Solution: blur (remove focus from) the element before you disable it.  Those sources that do refer to this specific problem tend to suggest that you focus a different element before you disable the text box.  But this is not necessary &#8211; you can just blur the element that has focus and leave the page with nothing focused.</p>
<h2>Problem test case</h2>
<p>Try clicking and typing in the field below &#8211; if your browser exhibits this problem, the text cursor will not appear.  If it does, then your browser does not have this problem.</p>
<div class="iframe-wrapper">
  <iframe src="/content/tech/files/2009/12/testcase_ffcursor1.html" frameborder="0" style="height:35px;width:300px;">Please upgrade your browser</iframe>
</div>
<p>This demo has a single text field which, when you click into it, is briefly disabled, and then enabled again.  The disabling of the field while it has focus triggers this problem, so the text cursor does not appear (but you can still type into the field!).</p>
<h2>Solution test case</h2>
<p>Now try typing in this field &#8211; the cursor should appear consistently.</p>
<div class="iframe-wrapper">
  <iframe src="/content/tech/files/2009/12/testcase_ffcursor2.html" frameborder="0" style="height:35px;width:300px;">Please upgrade your browser</iframe>
</div>
<p>This demo still briefly disables and re-enables the text field when it gets focus, but this time it blurs it right before it&#8217;s disabled, then focuses it again after re-enabling it.  The result is that when you place your mouse cursor in the field to manually give it focus, the text cursor appears as normal.</p>
]]></content:encoded>
			<wfw:commentRss>http://assanka.net/content/tech/2009/11/25/disappearing-text-cursor-in-firefox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Auto growing textareas</title>
		<link>http://assanka.net/content/tech/2009/05/04/auto-growing-textareas/</link>
		<comments>http://assanka.net/content/tech/2009/05/04/auto-growing-textareas/#comments</comments>
		<pubDate>Mon, 04 May 2009 16:24:52 +0000</pubDate>
		<dc:creator>Andrew Betts</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[User experience]]></category>
		<category><![CDATA[User input]]></category>

		<guid isPermaLink="false">http://assanka.net/content/tech/?p=3</guid>
		<description><![CDATA[This feels like a topic that&#8217;s been explored to death already, but I really don&#8217;t like most implementations of this technique, so here&#8217;s how we do it.
First, in case anyone has just arrived from Mars, or even more unlikely, isn&#8217;t familiar with Facebook, the auto-growing textarea is a text box that gets bigger as you [...]]]></description>
			<content:encoded><![CDATA[<p>This feels like a topic that&#8217;s been explored to death already, but I really don&#8217;t like most implementations of this technique, so here&#8217;s how we do it.</p>
<p>First, in case anyone has just arrived from Mars, or even more unlikely, isn&#8217;t familiar with Facebook, the auto-growing textarea is a text box that gets bigger as you type into it, so that you never see a scroll bar (unless you&#8217;re typing war and peace).</p>
<p>There are lots of ways of doing each part of this.  The parts I&#8217;ll look at are (a) what triggers a review of the text box&#8217;s size, (b) how to determine whether a resize is required, and (c) how to perform a resize.</p>
<h2>Triggering a review</h2>
<p>There are four ways this is typically done:</p>
<ol>
<li> On an interval, say every 50ms</li>
<li> On keypress</li>
<li> On change</li>
<li> On keyup</li>
</ol>
<p>I&#8217;m staggered by how many scripts insist on doing this kind of thing on an interval.  Setting up interval timers for no good reason is a shortcut to terrible performance and memory leaks galore.  So we won&#8217;t be doing that.  onkeypress and onchange events are triggered before the box is updated with the latest keypress, so we want to avoid those, as the latest keypress might have been the one to bring us onto a new line.  That leaves onkeyup, which is fired after the box is updated with the new character, and allows us to inspect it and decide whether to increase its size.</p>
<h2>To resize or not to resize</h2>
<p>How to determine whether to resize the box comes in three flavours:</p>
<ol>
<li> Count the number of newline characters in the textarea&#8217;s value, and see if that matches the number of rows the textarea has.</li>
<li> Create a &#8217;shadow&#8217; DIV which is off screen (eg. margin-left: -10000px) and has no height declared, but otherwise has the same style properties as the textarea, fill it with the textarea&#8217;s content, measure the subsequent height of the DIV, then see if that height matches the current height of the textarea.</li>
<li> Check whether scrollHeight (the height of the scollable content of the box) &gt; clientHeight (the height of the box itself).</li>
</ol>
<p>In this case I was suprised at the number of implementations that favoured counting newlines.  This only works if combined with counting the number of characters in the textarea&#8217;s value, AND a monospaced font, AND knowing the number of columns in the textarea.  In short, er, it&#8217;s mad.</p>
<p>Second option, and the one favoured by a lot of the framework plugins due to the ease with which you can create shadow elements in the likes of jQuery, is to create a shadow DIV. This has the advantage of telling you the actual pixel height of the text, even where it is LESS than the height of the textarea box.  Otherwise, you&#8217;re limited to measuring clientHeight and scrollHeight, which are exactly the same if the textarea isn&#8217;t scrolling, regardless of how much space you&#8217;ve got to spare at the bottom.  My issue with this method is that it basically requires use of a framework to not be painful, and even then it&#8217;s non-trivial amounts of code, and adds needless pollution to the DOM.</p>
<p>So that leaves relying on scrollHeight and clientHeight.  These are well supported and efficient to read, so provided that you work around the issue of scrollHeight always being at least equal to clientHeight, this offers a very lightweight solution.  The features that you can achieve with a shadow DIV that you can&#8217;t do by simply reading scrollHeight and clientHeight are (a) you can ensure there is always a blank line at the bottom of your textarea, and (b) you can shrink the textarea if the user deletes text from it.  I&#8217;m personally of the view that neither of these is actually particularly desirable.  There&#8217;s potentially an argument for leaving a blank line at the bottom, but equally the uer might feel like they&#8217;re just being pressured to write more.</p>
<h2>How to resize the box</h2>
<p>OK, so if you&#8217;ve concluded that the user is writing chapter and verse and the textarea is in need of a bit more space, how do we go about doing it?</p>
<ol>
<li> Animate the CSS height property</li>
<li> Add some height via the CSS height property</li>
<li> Add 1 to the rows attribute</li>
</ol>
<p>A fair few of the framework plugins use their framework&#8217;s animation capabilities to animate the grow effect.  I don&#8217;t like this.  Just because you have an animation effect available doesn&#8217;t mean it&#8217;s appropriate to use it, and there are many situations where the end user just doesn&#8217;t want to wait 300ms for the privilege of using a fresh line.</p>
<p>Simply adding height to the CSS property is a fair way of doing it, but unless you do some maths on the user&#8217;s line height (or hard code some magic numbers), you can&#8217;t necessarily guarantee that the resulting height will be a multiple of the textarea&#8217;s line height.</p>
<p>Easiest, most efficient solution: add one to the rows attribute of the textarea.  rows is part of XHTML as well as HTML 4.01, and has universal support going back yonks.</p>
<h2>Pasting</h2>
<p>Watch out for pastes.  If the user pastes in a large quantity of text, they will trigger only one keyup event, but will have added many lines to the textarea.  Make sure that if a resize is required, you trigger another review after the resize is complete.</p>
<h2>Max height: the war and peace scenario</h2>
<p>If the user really does seem to be writing a novel, we probably should call it a day on growing the textarea at some stage, and certainly <strong>before it gets to be taller than the viewport</strong>.  You can check the height against the viewport height, though I typically just restrict it to an arbitrary height, say 30 rows.</p>
<h2>The code</h2>
<p>You&#8217;ll need a textarea element with an ID of <code>mytextarea</code> to make this code sample work, and obviously you can easily modify it to use selectors from your favourite framework rather than the native <code>document.getElementById</code>.</p>
<pre class="brush: jscript;">
document.getElementById('mytextarea').onkeyup = function() {
	var ta = document.getElementById('mytextarea');
	var maxrows = 30;
	var lh = ta.clientHeight / ta.rows;
	while (ta.scrollHeight &amp;gt; ta.clientHeight &amp;amp;&amp;amp; !window.opera &amp;amp;&amp;amp; ta.rows &amp;lt; maxrows) {
		ta.style.overflow = 'hidden';
		ta.rows += 1;
	}
	if (ta.scrollHeight &amp;gt; ta.clientHeight) ta.style.overflow = 'auto';
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://assanka.net/content/tech/2009/05/04/auto-growing-textareas/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

