DOM scripting Health and Safety
My favourite pieces to commission when I jobbed as a packer at a chainsaw factory were the health and safety instruction videos. "Never check the level of petrol by holding a lighter to the opening" and "Never keep the blade between your legs when trying to start the chainsaw" were just two of the highlights.
Health and safety measures are important – they ensure that our work environment is enjoyable and will not make us sick. Just because our job is handling user agents and typing funky words into an editor does not mean we shouldn’t follow some of our own.
Right now, DOM scripting with JavaScript is hot again (the over 3000 unique hits on my post about outdated JavaScript techniques proved that to me) and we run into the danger of overusing it or using it in the same obtrusive ways we used JavaScript when DHTML was the flavour of the month.
When your tool is a hammer, everything looks like a nail, or – in some cases – a thumb.
Let’s not bash anything in sight or mince our own thumbs. DOM and JavaScript are there to enhance the structure and interact with the presentation of a web site – and not replace them.
Ideas that prevented me from repeatedly hitting my thumb:
- Don’t create HTML that should be there without JavaScript. Re-use what is already in the page instead. An accessible site starts with a semantic, well-structured HTML document. If that is not given, there is no chance we can be accessible. Not creating HTML via DOM makes the product a lot easier to maintain. Non JS-savvy colleagues won’t have to butcher your code to make a change on the page.
- Generated HTML follows the same rules as written HTML: Don’t create invalid HTML. We stopped using CSS to make elements look like headers instead of using real header elements. The same applies to the DOM - redundant HTML elements to fix a design or add a design feat are – well, redundant, no matter what technology was used to add them. By the way, this also applies to server side scripting.
- Be aware of visitor and user agent restrictions. An example are my fabulous clickable headers of the Unobtrusive JavaScript course. They look handy, work cross-browser and were developed with a clear separation of structure, presentation and behaviour. Where they fail is when you try to use them with a keyboard. The power of the DOM seduced me to turn a header into a hover-able and clickable element, but there is no way to use it without a mouse. That is why I will add a real interactive element to the headline – a link – to make them work for keyboard users (and possibly screen reader users) as well (thanks must go to Stéphane for flagging that up to me).
- Understatement is class. Before adding all kind of cool new JavaScript features and make everything hover, click and move it is a good idea to lean back and think: "Is this really necessary? Do I add it to help the user or do I add it because it is cool?"
- Don’t break too many conventions. As posted earlier, I see the web as a secondary media, and assuming that the visitor pays detailed attention to our web sites is more narcissism than reality. If we need to explain functionality to the visitor in a piece of text, there is a big chance that we will confuse rather than help. Example? Users hitting the back button on AJAX apps and pure Flash sites.
- Leave a clean desk. Especially in distributed developments it is of utmost importance that everybody speaks the same "code language" and that handovers are painless and quick as the code is already properly documented. Define an in-house coding standard, comment your code where applicable and there will be a lot less stressed faces and moaning when people get assigned to projects.


June 24th, 2005 at 3:39 am
June 23rd, 2005 at 8:01 pm
Thanks for your thanks, again :)
You know that the muse hovering over your shoulder these days makes me want to translate every post of yours into French? You’re absolutely right to say “I see the web as a secondary media, and assuming that the visitor pays detailed attention to our web sites is more narcissism than reality.”
We’re the only ones, in the small web design circle, to be so focused on those small details.
(at the same time we have to, it’s their effectiveness that makes the experience easy and seamless…)
June 24th, 2005 at 8:27 am
So very true. I especially appreciate (1) and (6) – good maxims are code maintainability and portability.
June 24th, 2005 at 10:16 am
Hello,
I completely agree with all that is said in this page. In fact, I mainly wanted to say to Stephane Deschamps and the author, that you’re not alone caring for such simple and small matters; I do too :) So we’re three :D
BTW, I like your site a lot, Stephane. It is well written, and with a very “refreshing feeling” to it. I’m bookmarking it.
Yves.
June 25th, 2005 at 1:28 pm
Some advice for #1:
When you are adding information to your Javascript like IDs, text, etc, the first thing you should ask yourself is “Can I get this information from the page instead of typing it into my Javascript?” If the answer is yes, pull it from the page instead. It’s one less thing to keep up to date.
If the answer is no, ask yourself “Should this information be part of the page?” Usually, if you need information for a caption or something, you should be including the information on the page for non-Javascript user-agents (e.g. Google), and temporarily hiding it until the extra information is requested. Then, your answer to “Can I get this information from the page?” should be yes.
It’s not always necessary that the information you include on a page is included as part of the visible page content. Often, the information belongs in an attribute. For example, instead of typing popup caption text for a photograph into your Javascript, store it in the title attribute instead.
Another thing I would add, that is applicable to both Javascript and CSS, is:
#7: Don’t be explicit about what is implicit.
There’s no need to have class=”menuitem” on every child of an element with id=”menu”. You can use contextual selectors in CSS and childNodes in Javascript to refer to the menu items. It’s completely useless code to include all those classes.
June 27th, 2005 at 5:39 pm
Read the article Six JavaScript features we do not need any longer now. It’s a good roundup on common JavaScript techniques which are totally absolete these days. Even if you don’t sign it, it’s definitely something to think about.
Oh, and when y…
August 22nd, 2006 at 1:17 am
I like your site a lot, Stephane. It is well written, and with a very “refreshing feeling” to it. I’m bookmarking it.
September 2nd, 2006 at 3:57 pm
In the beginning I`ve loved Java-Script to much, now I make sites striktly without.
I agree 100%! (point 1-6)