Archived entries for javascript

To Be, or Not to Be, an Anchor

In reading Chris Coyier’s response to a reader’s question on when to choose an anchor element over some other element when creating javascript-driven experiences, I realized that only recently did I form a concrete opinion on the matter. My response originally started as a comment on Coyier’s post, but quickly turned into a full post of its own.

Here’s the part of Chris’s response with which I identified most:

If the app is totally JavaScript dependent all behavior is attached via JavaScript, I guess it doesn’t really matter what element you use.

Until recently, I would have given the same answer. For the past year and a half I’ve been part of a large team working on the rewrite of a major ecommerce site that required full accessibility for blind, low-vision and cognitively disabled users. Being a javascript-heavy UI, it required a lot more effort in discerning the semantics of an HTML element.

I plan on writing a lot more about that experience, but right now I want to respond specifically to the question of when to use an anchor element.

The reader says that he generally used the a tag liberally to represent an element as clickable.

In brief, that’s the safest choice. But the natural inclination of a developer (ideally) is to dig as deep as possible and make the most informed choice. After all, their the ones who have to support it when things get wonky.

If you decide on something other than an anchor though, just make sure the element you choose is accessible to keyboards and screen readers. Easy. Done.

Of course, it can be a lot more complicated than that. Anchor elements are natively focusable and screen readers provide functionality for navigating a page by its anchors. This allows to users to quickly scan the page via their keyboard. Visual users often (and understandably) take for granted their ability to visually scan a page since it’s a natural response and not specific to viewing a webpage. But, substituting a different element can require a lot more effort in facilitating a equal experience to non-visual users.

Using tabindex with a value of 0 on a non-natively focusable element will allow for the element to be navigated via the tab key, and in the order of the natural document flow. But the catch is that a screen reader will not include the element in its anchor shortcut feature. This may not be necessary if the the element’s purpose would not make sense outside of its surrounding context, like a tooltip, for example.

More importantly though, the screen reader will not speak the element as a link. Visual users employ visual clues to interpret actionable elements in a page. Screen readers rely on semantics. While a visual user can instantly recognize a button by its design, a screen reader can do so only if a button element is used.

Also, it’s good to understand that title attributes are not necessarily spoken when a screen reader user is focused on an element that contains one. The popular screen reader JAWS provides this as an op-in preference. So using a span as an actionable element will need both a title attribute and screen-reader-only text (off-screen) to communicate to the user the purpose of the element.

Ultimately, an anchor element provides users an actionable element that’s accepted as a means to navigate information. Regardless of whether the navigation happens via a full page refresh, asynchronously, or with javascript turned off, the underlying meaning remains the same. Using it liberally is a good choice.

Update: 8.24.2011

Regarding ARIA, support is getting better, but it’s still pretty spotty. In fact, I’ve had to remove ARIA attributes at times because of screen reader conflicts. It can also change how the user’s screen reader interacts with a page (Form and Application modes). This can be disorienting to a user who has yet to master the full feature set of their screen reader. But after reading Dave’s comment below, I figured I should give the link role a try. I use the role when it’s useful, but the link value is one I’ve never considered.

I used the following markup for my quick test:

<span id="link" role="link" tabindex="0">Test Link</span>

Surprisingly, JAWS 12 with IE8 spoke the element as a link. I was also able to navigate to it using the u key shortcut (navigates unvisited links). The v key did not register it as a visited link even though it had been clicked. That makes sense since there’s no href. Good to know.

Update: 11.8.2011

I’ve put up a page of various attempts to mimic links. Not exhaustive, but should be helpful in drawing general conclusions. I’ll add to it as I get more examples and time.

Toggle Class – The jQuery Way vs. The Native Way

I work with a team of developers located in the US, Canada and Bangalore. On behalf of the North American team, I send daily status emails detailing what each dev worked on, as well as their svn commit log, so the offshore devs and project leadership can stay synced.

For the past 2 or 3 weeks I’ve been including little quick tips on CSS and javascript. The team I’m on is composed of people with a variable level of skills and this is an attempt to affect those levels.

I thought I’d start posting some of these to archive ones I would have found helpful when I first started programming.

We use jQuery in our project and the majority of the devs (including myself) learned javascript via jQuery. Below is what it takes to toggle a class using jQuery vs. native javascript. As you can see, we’ve had it easy.

The jQuery Way

$( '.some-class' ).toggleClass( 'newClass' );

The Native Way (I’m sure there are better ways):

var i = 0,
    elements = document.getElementsByClassName('some-class'),
    len = elements.length,
    classToggle = function( element, tclass ) {

        var classes = elements[i].className,
            pattern = new RegExp( tclass );
            hasClass = pattern.test( classes );

        classes = hasClass ? classes.replace( pattern, '' ) : classes + ' ' + tclass;
        elements[i].className = classes.trim();

for ( ; i < len; i++ ) {
    classToggle( elements[i], 'newClass' );

The native example could be wrapped up a bit tighter to make it easily reusable. One of the reasons the jQuery example is so concise is that it relies on it's own methods for making collections and performing loops. The $ and toggleClass functions have quite a bit of code backing them up. If I were to add in all of the source code jQuery uses to preform these tasks, things would look a lot different.

But that's not the point. The thing that has enabled me to use jQuery as just one of the tools in my toolbox, instead of the only tool, is understanding the underlying functionality.

A couple of good references: the jQuery source (of course) and the MDC.

Interesting Links

Programmer Competency Matrix

I was surprised to find that I placed in level 3 for most of the “Programming” rows (as I understand my skills anyway :). Art school has definitely made for some strange bedfellows.

It was motivating to see where I placed in all of the topics, as my approach to learning programming has been to aquire as much understanding as possible about the the things I don’t know. Finding that I was at level 2, but knowing the existence of the concepts in level 3 validates that this approach is moving me in the right direction.

I likened this philosophy to Elizabeth the other day as analogous to how one understands their city. We live in LA, a big, sprawling metropolis, and while I’ve never been to Monrovia or Cerritos (far out there), I understand that both are cities in Los Angeles County. Not the best example, sure, but one that illustrates the differences in approach. I actively seek out and retain this type of understanding in the belief that it may be useful at some point. Others my see it as the inevitable result of living in the same place a long time.

A better example would be to relate it to one’s chosen carrer. Using this angle, it becomes quickly evident that the ones who take an active approach are usually more successful (the definition of the success is based on the specific domain, of course). Conversely, those taking a passive approach tend to exert more effort in defending how what they do know is more tried and true, and hence more reliable. More dangerously, as a superior, they tend to actively block their peers and subordinates. Anyone who has worked in a university or college setting will loudly attest.

Deep Linking with Javascript

Open sourced javascript implementation for linking to paragraphs, and even sentences, in webpages. Uses a matching pattern based on the first letters of the first 3 words in the first and last sentence of the paragraph. That was a mouthful. For example, the key for this paragraph would be [OsjFet].

A fun way to practice your Dvorak skills

I still intend to pick up proper typing. And when that time comes, it will definitely be on a Dvorak keyboard.

I started the typing tests last year and unfortunately, lasted only a week. It takes the same discipline as QWERTY (no surprise there), but I find the layout much easier to memorize. Now, if Apple could get on the ball with creating a beautifully designed split keyboard, I could get rid of this clunker. It’s hideous, poorly constructed and loud (key paddings wear out quickly), but it’s still the most comfortable I’ve found.

Copyright © Ryan Fitzer 2011. All rights reserved.

RSS Feed. This blog is proudly powered by Wordpress and uses a modified version of Modern Clix, a theme by Rodrigo Galindez.