weboholic.de

random rant-o-rama

Unobtrusive Smilies :)

without comments

I don’t know whether you may find use for this, it might be a complete overhaul, but anyway.

Smilies are small inline images that represent a mood like being sad :( or help enrich the context with some emotion like winking after a joke ;). Everybody uses them but no one actually puts down those img-tags: we rely on our smart CMS’s to the job. Smilies are accessible since screen readers use either the title or the alternative text attributes of the img-tag. The problem is that such inline images aren’t semantically correct: they should be part of the text as text and not as images.

I’ve put down a small (~2KB minified) mixture which filters all smilies and replaces them with images in an unobtrusive way.

Download uns v. 0.2

What is the basic?

Default behavior is to query the document for span-tags that have the class smiley. Each one option is customizable.

What’s the compatibility?

It’s known to work with Firefox 2, Firefox 3, IE5.5, IE6, IE7, Safari 3 and Opera 9 under Windows, Safari 3 under Mac OS X and Konqueror 3.5 under Kubuntu.

How to trigger it?

Use it just as any other unobtrusive JavaScript out there:

  1. window.onload = function() {
  2.   uns.parse();
  3. };

Or gain performance by providing a node where smilies are to be found, like the main content div-block:

  1. window.onload = function() {
  2.   uns.parse({node: document.getElementById('content')});
  3. };

You can also provide an onfinish callback:

  1. window.onload = function() {
  2.   uns.parse({
  3.     node: document.getElementById('content'),
  4.     onfinish: function() {
  5.       alert('finished');
  6.     }
  7.   });
  8. };

Or you can add additional smilies:

  1. window.onload = function() {
  2.   uns.smilies({
  3.     ':cska:': 'images/cska.gif'
  4.   });
  5.   uns.parse();
  6. };

You can also AJAX load new content into some poor div and run the parser afterwards:

  1. xhr.onreadystatechange = function() {
  2.   if (xhr.readyState == 4) {
  3.     var comments = document.getElementById('comments');
  4.     comments.innerHtml = xhr.responseText;
  5.     uns.parse({node: comments});
  6.   }
  7. };

Where can it be seen?

I’ve put down a small demo.

Log

20.02.2008: 0.1 Initial release.
22.02.2008: 0.2 Added support for native getElementsByClassName and tested with Firefox 3, added support for IE5.5 and IE6.

Written by Никола

February 20th, 2008 at 11:01 am

Posted in Development

Tagged with , ,

Leave a Reply

Clicky Web Analytics