Archive for February, 2007

Improved unobtrusive linked select boxes

Here’s a problem that crops up regularly in Web interfaces: having a dropdown whose available options depend on another dropdown. The canonical example, if you like, is the date selector:

Select date

This has the obvious (though not serious) problem that one can select, say, February 31st. We’d like to catch those errors by having the values in the “day” select depend on what is chosen in “month”. It’s easy to do this on the server with a round-trip, of course, but that has the problems that the user interface can be in an inconsistent state before the form is submitted; it would be nice to be able to finesse the interface, using JavaScript, to remove that possibility.

We can do this ad-hoc, of course, but that needs specialised code for each instance (well, for each type of data and dependency); a general solution would be better. We’d also like it to be unobtrusive, by which I mean two closely related things:

  1. The markup is meaningful
  2. It works without JavaScript

Bobby van der Sluis gives a pretty good general solution. Briefly, the technique involves keeping a full set of the dependent options and picking the appropriate options from it when necessary.

It relies on encoding the dependency as an HTML class: to me it’s a muddy use of class, but the real problem is that it’s not a very obvious way of making the dependency explicit in the markup. I think we can improve on it. Using OPTGROUP we can produce straight-forward markup that makes the dependency obvious:

      <form>
      <fieldset>
        <legend>Select date</legend>
        <label for="month">Month</label>
        <select id="month">
          <option value="jan">January</option>
          <option value="feb">February</option>
          <option value="mar">March</option>
          ...
        </select>
        <label for="day">Day</label>
        <select id="day">
          <optgroup label="January">
            <option value="1">01</option>
            <option value="1">02</option>
            <option value="1">03</option>
            ...
          </optgroup>
          <optgroup label="February">
          ...
        </select>

Without JavaScript it looks like this:

Select date

Here’s my (allegedly) improved unobtrusive linked select box code (it uses MochiKit to avoid some verbose DOM manipulation — just read $(x) as “select the element with ID ‘x’”, and the rest does what it says):

function linkSelects(parent, child) {
  var parent = $(parent);
  var child  = $(child);
  var cloned = child.cloneNode(true);
  removeEmptyTextNodes(cloned);
  refreshDynamicSelectOptions(parent, child, cloned);
  connect(parent, 'onchange', function(event) {
    refreshDynamicSelectOptions(parent, child, cloned);
  });
}

function refreshDynamicSelectOptions(parent, child, optionholder) {
  var alreadySelectedValue = (child.selectedIndex >= 0) && child.options[child.selectedIndex].value;
  replaceChildNodes(child);
  var selectedLabel = strip(scrapeText(parent.options[parent.selectedIndex]));
  for (var i=0; i < optionholder.childNodes.length; i++) {
    var opt = optionholder.childNodes[i];
    if (opt.tagName.toLowerCase() == "option") {
      var newopt = opt.cloneNode(true);
      if (newopt.value == alreadySelectedValue) newopt.selected = true;
      appendChildNodes(child, newopt);
    }
    else if (opt.tagName.toLowerCase() == "optgroup" && opt.label==selectedLabel) {
      for (var j=0; j < opt.childNodes.length; j++) {
    var newopt = opt.childNodes[j].cloneNode(true);
    if (newopt.value == alreadySelectedValue) newopt.selected = true;
    appendChildNodes(child, newopt);
      }
    }
  }
}

and lastly, here’s the working version:

Select date

There remain a couple of weaknesses. It relies on the convention of OPTGROUP labels being the same as OPTION text; it’s reasonable, since there is a semantic link between those two things. Also, it doesn’t always get selection right — easily seen in this example, where you’d expect a choice of ‘01′ to persist when changing months. I think those are tweaks away.

Add comment February 21st, 2007 mikeb

A crypto standards manifesto

Our very best examples of crypto standards in commonplace use are so poor it’s no wonder that developers end up hand-rolling their own broken alternatives. It’s time we offered them something better.

Continue Reading 3 comments February 20th, 2007 Paul Crowley

JSON and JSON-RPC for Erlang

About a month ago, I wrote an implementation of RFC 4627, the JSON RFC, for Erlang. I also implemented JSON-RPC over HTTP, in the form of mod_jsonrpc, a plugin for Erlang’s built-in inets httpd. This makes accessing Erlang services from in-browser Javascript very comfortable and easy indeed.

Continue Reading 7 comments February 17th, 2007 tonyg

An Alphabetical Google Zeitgeist

I’ve installed Firefox 2.0 on most of the machines I work on daily now. Its use of google suggestions surprised me when I first saw it, but I’ve grown to find it somewhat useful on occasion now. It suggested the following experiment (not in so many words, of course): assuming that the suggestions it supplies are based on popularity of search terms (presumably filtered by google’s safe-search feature!), then the suggestions ought to reflect the zeitgeist to a certain extent - what does it suggest for each letter of the alphabet? The results weren’t exactly surprising. No philosophy, very little science, technology, literature or art; nothing but wall-to-wall Britney, Ebay and “U tube” (!). The A-to-Z follows below.

This list was taken from the top results from the google search suggestion box in Firefox 2.0 on 30 Jan 2007:

  • A is for Amazon
  • B is for Britney Spears, Best-Buy
  • C is for Circuit City, Craigslist
  • D is for Dictionary, Dell
  • E is for Ebay
  • F is for Facebook
  • G is for Google, Gmail, Google Earth, Google Maps, …
  • H is for Hotmail
  • I is for IMDB, iPod
  • J is for John Lewis, JCPenney and Jobs
  • K is for Kohls, K-Mart
  • L is for Lyrics, Limewire
  • M is for Myspace
  • N is for Nintendo Wii
  • O is for Office Depot
  • P is for Paypal
  • Q is for Quotes
  • R is for Runescape
  • S is for Sears, Staples, Skype
  • T is for Target, Toys-R-Us, Tesco
  • U is for U Tube (sic), UPS
  • V is for Video, Verizon Vodafone and Virgin
  • W is for Wikipedia, Walmart
  • X is for Xbox 360
  • Y is for Yahoo, Youtube
  • Z is for Zip Codes (and after a few places, oddly, Zilog)

1 comment February 17th, 2007 tonyg

RFC 1982 limits itself to powers of two unnecessarily

RFC 1982 defines a “Serial Number Arithmetic”, for use when you have a fixed number of bits available for some monotonically increasing sequence identifier, such as the DNS SOA record serial number, or message IDs in some messaging protocol. It defines all its operations with respect to some power of two, (2^SERIAL_BITS). It struck me just now that there’s no reason why you couldn’t generalise to any number that simply has two as a factor. You’d simply replace any mention of (2^SERIAL_BITS) by, say, N, and any mention of (2^(SERIAL_BITS-1)) by (N/2). The definitions for addition and comparison still seem to hold just as well.

One of the reasons I was thinking along these lines is that in Erlang, it’s occasionally useful to model a queue in an ETS table or in a process dictionary. If one didn’t mind setting an upper bound on the length of one’s modelled queue, then by judicious use of RFC 1982-style sequence number wrapping, one might ensure that the space devoted to the sequence numbering required of the model remained bounded. Using a generalised variant of RFC 1982 arithmetic, one becomes free to choose any number as the queue length bound, rather than any power of two.

2 comments February 17th, 2007 tonyg

Rabbits, rabbits, rabbits

We’re proud to announce that the project we’ve been working on for the past few months, RabbitMQ, has been released. RabbitMQ is an AMQP server written using Erlang/OTP. Check it out at http://www.rabbitmq.com/ - or you can go straight to the downloads page for sources and binaries.

1 comment February 1st, 2007 tonyg

Calendar

February 2007
M T W T F S S
« Jan   Mar »
 1234
567891011
12131415161718
19202122232425
262728  

Posts by Month

Posts by Category