All posts by Lachlan Hunt

XBL Part 2: Event Handlers

This is part 2 in my 5 part series on XML Binding Language. If you haven’t already read part 1, I suggest you do so now before you continue reading. XBL provides a mechanism for attaching event listeners to elements, and declaring the conditions under which the handlers should be invoked. In this article, I’m going to compare traditional event handling techniques with those that will be provided using by XBL.

Traditional Event Handling

The following example illustrates some typical unobtrusive scripting techniques to attach event listeners, including both the window.onload property and the addEventListener() function.

window.onload = function() {
    var nav = document.getElementById("nav");
    var li = nav.getElementsByTagName("li");
    for (var i = 0; i < li.length; i++) {
        li[i].addEventListener("mouseover", doSomething, false);
    }
}

Another common method, though generally considered bad practice these days, is to use the HTML onevent attributes, like the following.

<li onmouseover="doSomething();">...</li>

There are advantages and disadvantages to both methods, but the former is generally considered better because it separates the behaviour layer from the markup. However, the latter is a simple declarative syntax that can be quite convenient in some cases.

Handling Events with XBL

In XBL, instead of requiring authors to use a script to search for the elements, the event listeners are attached to those that the binding is attached to. XBL provides a simple declerative syntax which also continues to separate the behaviour layer from the semantic markup layer. Event listeners are declared using both the handlers element and its child handler elements.

In the previous article, we looked at how bindings are associated with elements using a selector, just like in CSS. For example, this binding will be attached to all li elements within an element with id="nav".

<xbl xmlns="http://www.w3.org/ns/xbl">
  <binding element="#nav li">
    <handlers>
      <handler event="mouseover">
        doSomething();
      </handler>
    </handlers>
  </binding>
</xbl>

If present, only one handlers element is allowed within a binding, but it can contain as many child handler elements as required, to capture as many different events as you like. This binding declares a single event handler that listens for the mouseover event. When the mouseover event is fired on a bound element (i.e. an element to which this binding is attached), the handler is invoked in effectively the same way it would have been using the other methods shown above.

Event Filters

There are often times when you only want to handle an event under certain conditions. For example, when you want to capture a click event and do something only when the user clicks the left mouse button; or capture a keyboard event and perform different functions depending on which key was pressed. In traditional scripting techniques, you have to check the values of certain properties using if or switch statements in your function, like the following.

function doSomething(e) {
    var code;
    e = e || window.event;
    code = e.keyCode || e.which;
    switch(code) {
        ...
    }
}

Much of that involves handling of browser incompatibilities, but even if all browsers supported the DOM Events standard, it’s still quite complicated. XBL addresses this by providing a simple declarative syntax for describing these conditions using attributes on the handler element.

In the following example, separate handlers are provided for for handling the keypress events depending on which character was entered. The first handles the character a, the second handles b. If any other character was entered, neither of these two handlers will be invoked.

<handlers>
  <handler event="keypress" text="a">
    doSomethingA();
  </handler>
  <handler event="keypress" text="b">
    doSomethingB();
  </handler>
</handlers>

Similarly, in the following example, the handler will only be invoked when the user left clicks while holding the Shift key down.

<handlers>
  <handler event="click" button="0" modifiers="shift">
    doSomething();
  </handler>
</handlers>

Other Common Event Filters

There are several other filters that can be used. The following list is a subset of the available attributes for this purpose. I suspect these will be the most commonly used filters because they cover the majority of mouse and keyboard event usage on the web today.

button
A space separated list of mouse buttons pressed by the user. e.g. button="0 2" matches either the left or right mouse buttons.
click-count
The number of times the user clicked. e.g. click-count="2" matches double clicks.
text
The text entered by the user. This is different from the key code because it matches the letter that was entered, regardless of the keys that were pressed. This is particularly important for languages that require several key presses to enter certain letters.
modifiers
Modifer keys, including alt, control, shift, meta, etc.
key
Matches against the keyIdentifier value defined in DOM 3 Events
key-location
For matching the location of the key that was pressed on the keyboard, including standard, left, right and numpad.

In the next article in this series, I’ll be taking a look at XBL Templates, which will provide significant presentaional enhancements, particularly in regards to layout.

XBL Part 1: Bindings

XML Binding Language (XBL) is a mechanism for extending the presentation and behaviour of a document. The XBL 2.0 specification recently reached Last Call and it has some very cool features to look forward to using in a few years. It’s somewhat based upon the original XBL 1.0 specification created and implemented by Mozilla, though it has been significantly redesigned and is not backwards compatible with it.

While reading this, keep in mind that XBL is still a working draft and any feature I discuss may change significanly between now and when it becomes a recommendation. Presently, there are no implementations of XBL 2.0, so you can’t use it yet.

Bindings

A binding is a way to attach presentation and behaviour to an element. The concept is similar to the way we already style elements using CSS and attach event listeners to them with JavaScript, but the idea is to add an extra layer of abstraction in between to simplify the process. Bindings are a not a way to replace existing authoring tools like CSS and JavaScript, but rather an enhancement to them.

There are four main aspects of a binding: implementations, templates, handlers and resources. In this whopping 5 part series, I intend to give you a brief overview of each of these components to explain their purpose and functionality.

Implementations
Describe a set of methods, properties and fields on an element.
Handlers
These offer an improved way to declare event listeners.
Templates
A way to enhance the presentation (particularly layout) beyond what is possible with existing CSS techniques.
Resources
Additional stylesheets, images, video, audio or any other content associated with the binding.

Sample Bindings

Bindings can be attached to elements in several ways: a selector in the element attribute of the binding element, the ‘binding‘ property in CSS or using a script.

The element Attribute

The element attribute specifies a selector. The same type of selector you use with CSS, so it’s very easy to understand. This binding will be attached to all elements that match the selector: #nav li.

<xbl xmlns="http://www.w3.org/ns/xbl">
  <binding element="#nav li">
    <implementation>...</implementation>
    <template>...</template>
    <handlers>...</handlers>
    <resources>...</resources>
  </binding>
</xbl>

The ‘binding‘ Property

The ‘binding‘ property can be used in in your CSS to attach a binding, in exactly the same way you apply any other other style to an element.

bindings.xml:

<xbl xmlns="http://www.w3.org/ns/xbl">
  <binding id="foo">
    <implementation>...</implementation>
    <template>...</template>
    <handlers>...</handlers>
    <resources>...</resources>
  </binding>
</xbl>

The stylesheet:

#nav li { binding: url(bindings.xml#foo); }

Using a Script

Elements will implement the ElementXBL interface, which defines three methods: addBinding(), removeBinding() and hasBinding(). The addBinding() method can be used to attach a binding to an individual element using a script, like this:

var e = ...; // Get the element somehow
e.addBinding("bindings.xml#foo");

Web Directions 2006

It was an absolutely phenomenal experience! Everything from the workshops and presentations to the people, and the after parties were just fantastic!

Accessibility 2.0 Workshop

On Wednesday, 2006-09-27, I went to Derek Featherstone’s workshop on accessibility. It was very informative and interesting to see many of the issues demonstrated. Throughout the day, he demonstrated the use of various assistive technologies, including a screen reader and voice recognition, showing just how badly common mistakes can affect the usability and accessibility of a site. Although I had previously read about several of the issues discussed, some were new to me and it was great to see the practical demonstrations.

Conference Day 1

Thursday, 2006-09-28, was day 1 of the conference. The most enjoyable presentations for me were Jeremy Keith’s Explaining Ajax (featuring a sexy, and rather hairy, topless photo of Cameron Adams) and John Allsopp’s Microformats. Derek also presented on accessibility, which was also good, but it was almost a subset of the topics addressed in his workshop, with only a few new points.

Conference Day 2

Friday, 2006-09-29, was the second and final day of the conference. At the beginning of the day, John Allsopp awarded prizes for cool and/or funny photos posted to flickr and . I was lucky enough to receive some prizes for my photo of Ben Buchanan in is Web 2.0MFG shirt. I won a SitePoint shirt that reads “Eat, Sleep, Code” and two books: The CSS Anthology and Deliver First Class Websites.

Andy Clarke’s presentation, Creating Inspired Design, was extremely interesting. As I’m a coder, not a designer, it was good to learn about web design from a designer’s perspective. Without showing any code at all, he illustrated ways to think about design and how to look for inspiration in the real world. He showed how, with just a little creativity, we can start to break out of the common 2 or 3 column layouts and into more interesting concepts.

However, the highlight of the day was Cameron Adams getting his revenge with a topless photo of Jeremy. But, according to Jeremy, that photo was actually photoshopped.

The People

Despite all the great presentations and workshops, the most exciting part of the conference was getting to meet so many wonderful people. It was an absolute pleasure to meet the likes of Molly Holzschlag, Derek Featherstone, Jeremy Keith and James Edwards (Brother Cake), as well as catching up with people I’d met before like Russ Weakley, Cameron Adams, John Allsopp and many others.

I got plenty of great photos over the 3 days I was there. In particular, the Lachlan³ photo featuring myself, Lachlan Hardy and Lachlan Donald, and myself with a bunch of fairly hot chicks.

Finally, the t-shirt slogan competition to come up with a Web 2.0 slogan is yet to be announced, but I’m hoping to win for my “Wake Me for Web 3.14” shirt. Of all the shirts I saw at the conference and those on Flickr, I’m predicting the major prize of a Sony MYLO will be awarded to either myself or Anson Parker for “Wallet Inspectr Nigerian Division”. John told me it would be announced on the Web Directions blog some time this week.