The Village Soup

The estimable Nathan Smith has just written a really interesting article on Digital Web Magazine called The Village Stew. Suffice to say I’ve been that chef on a number of occasions, and it’s galling to see what would otherwise be a great dish get messed up by the inclusion of myriad random ingredients. As these guys would say; keep it simple.

It’s a shame that web designers and developers are seen in this way – as people with unimpressive skills, a commodity. After all, the world is being changed precisely because of what we make happen (even if the original ideas may come from someone else). Nathan outlines some of the problems we can face when working in a large organisation, and some ways we can mitigate against having our work over-commiteeised.

Of course not all projects and clients are going to be a dream to work with, so we need to be prepared for the problems we could encounter along the way. Until web developers and designers are rightly given the same respect as, say, architects, we’ll just have to take the rough with the smooth.

Unobtrusive Javascript – the stillbreathing way

It’s been a while since I posted a decent code sample in JavaScript, so here’s a quick idea that I’ve been formulating for a while. It’s to make JavaScript unobtrusive – which means it doesn’t clomp it’s heavy boots all over your nice flowers. Sound too good to be true? Not really.

For this example I’ll be using the Prototype library (take a look at their shiny new website, too) as it’s my framework of choice. There are plenty of other frameworks available any one of which will work with this technique, so don’t worry if you’ve got a hankerin’ for another flavour of framework.

OK, onto the article. The problem is that I want to attach a JavaScript command to certain elements, in this case a series of links, the command in question will load content from an external resource into the page at a specified point in an AJAXy manner. Well, actually it will be an AJAHy manner, as we’re using HTML rather than XML as the return data type. What does that mean to you? That you don’t have to do any Javascript transformations of XML data. Vive la innerHTML.

So, first things first. We slip a little onClick goodness into our links … er, hang on. No we don’t – that would be extremely intrusive. So what can we do? Let’s think about what we need to do with this link:

  1. Make the browser aware it’s a special type of link so that we can access it with our Javascript.
  2. Somehow make the link set the Javascript content loader function to access the right server-side script. Hmm.
  3. Somehow make the link tell the Javascript content loader function to put the content returned from the server-side script into a certain element. Hmm again.
  4. Not be shown if the user doesn’t have Javascript enabled/available.

The first thing we do is group all the special links together by giving them a special CSS class name. In this case as our links are going to be refreshing parts of the page we’ll use a CSS class of ‘refresher’. That makes our links Refresher links, and everyone likes refreshment, don’t they?

So we’ve got our class set on all our Refresher links, which means we can use the Prototype DOM function getElementsByClassName to access them all. But what about hiding the links when Javascript isn’t available.

Simple. Let’s set some CSS in our stylesheet to hide the Refresher links by default:

.refresher
{
  display: none;
}

And they’re gone! Now we just need to turn them on again using Javascript – that means only people with Javascript will see them. Here’s the CSS:

.refresheractive
{
  display: inline;
}

And here’s the Javascript:

for (var i=0; i<document.getElementsByClassName('refresher').length; i++)
{
  document.getElementsByClassName('refresher')[i].removeClassName('refresher');
  document.getElementsByClassName('refresher')[i].addClassName('refresheractive');
}

So now those links will only show for people with Javascript. Great. But what about the other two bits of information we need to pass from the link – the processing server-side script and the target element? Well, here’s where we dust off some little-used attributes. Namely rel and rev

I should say at this point that rel and rev do have specific semantic meanings, denoting the documents’ relationship with the target of the link. They are used quite a lot for microformats, and rightly so, but I don’t think this technique is stepping too far away from correct use of those attributes. And at least this way is better than making up non-standard attributes (I’m looking at you, Spry) or forcing convoluted encoded strings into the links id attribute.

So, how do we do it? Easy, like this:

<a href="#targetElement" id="MyFirstRefresherLink" rel="updater.php" rev="targetElement">Click to update</a>

See? Easy. A bit of Javascript magic and we’re up and running.

for (var i=0; i<document.getElementsByClassName('refresheractive').length; i++)
{
  var targetElement = document.getElementsByClassName('refresheractive')[i].getAttribute('rev');
  var processingScript = document.getElementsByClassName('refresheractive')[i].getAttribute('rel');
  // do the updater thingy here
}

That technique works great for me, so I hope it’s useful for you.

Invaluable CSS techniques (and people)

Here’s one for you CSS jockeys to bookmark: 53 CSS techniques you couldn’t live without. I knew about half of these, but the others will come in really handy as well.

UPDATE: If you’re looking for the best of the best CSS gurus to handle your large web project this bunch will do you proud (tell them I sent you, they’ll say "Chris who?"). But if you just need a small project doing then you could hire me (tell me I sent you, he’ll give you a good deal).

Link-o-rama

I’ve had a folder full of text files containing links to interesting stuff for months now, and I’m just not going to have time to write full blog posts about all of them. So here are a few of them, with some quick thoughts from me.

1) theundersigned.net/2006/06/why-business-blogs-are-important/
It’s pretty much all there in one page. Print it and leave it on your bosses desk.

2) redcouch.typepad.com/weblog/2006/04/too_much_good_s.html
If you’re going to get into this business, you’d better make sure you really are unique – or streets ahead of any competitors.

3) escapefromcubiclenation.com/get_a_life_blog/2006/05/all_the_worlds_.html
When things are getting you down, it could always be worse. At least you still have your trousers on. (You do, don’t you?)

4) redcouch.typepad.com/weblog/2006/04/individual_vs_t.html
Team blogs can be good, but they aren’t always the answer.

5) domscripting.com/blog/display/69
A great little solution to a simple problem.

6) marketposition.com/blog/archives/2006/07/seo_in_a_nutshe.html
Does what it says on the tin: search engine optimisation in a nutshell.