David Emery Online

Hi there, I’m David. This is my website. I work in music for Apple. You can find out a bit more about me here. On occasion I’ve been known to write a thing or two. Please drop me a line and say hello. Views mine not my employers.

Signup to receive the latest articles from de-online in your inbox:

Go javascript, go!

30 March 2006

Don’t use Javascript to hide bad markup (from the Muffin Research Labs)

The article above raises some interesting points, some of which I agree with and some I don’t. I wholeheartedly agree that using javascript to insert invalid markup just so your site validates is fools work; just because the validator says it’s ok, doesn’t mean it actually is. If you go to the trouble of trying to conform to a specific doctype, then you should be using the mark-up it supports, and not hacking around aspects it doesn’t (normally for a very good reason).

However, I’m not sure about not using javascript to insert valid html. The example used is one of the many javascript libraries that turn a normal div into one that has rounded corners – these normal work by adding – via javascript – extra divs/other tags to the html code. I don’t really have a problem with this – the reasons for not adding extra divs and elements are mostly with regards to separating content and presentation, making the code easier maintain and also to help with accessibility (so screen readers and the like don’t choke on your pages).

Now, firstly; who’s to say that the presentation layer has to be purely in css? We can still separate the presentation aspects off from the content by using javascript as part of the presentation layer – in what way does javascript changing the look of the page differ from css changing it?

Obviously these methods don’t affect the maintainability of the underlying code – the best way of utilising these types of javascript manipulations is by using ids and classes and completely separating it off from the html in separate files. Whether they affect accessibility or not is a complicated issue; screen readers these days are not simple text browsers – some do process javascript, but whether these techniques would negatively impact there use is unclear (and also highly dependant on the code you insert).

So, in short: don’t use javascript to bypass the validator to insert invalid code, but inserting code isn’t necessarily such a huge sin.