Muffinresearch Labs by Stuart Colville

Don’t use Javascript to hide bad markup | Comments (13)

Posted in Code on 30th March 2006, 7:30 pm by Stuart

In a recent post on 456 Berea Street, Jeff Croft made a very valid comment suggesting that using JavaScript to create invalid markup was no better than writing invalid markup in the first place. This is something I totally agree with. The rising popularity of JavaScript following the buzz surrounding by Ajax has meant more and more sites are using javascript more fully, but it’s important that developers don’t get sloppy just because modifications to the document aren’t visible in the source.

Using JavaScript to add non-semantic markup

In my opinion JavaScript shouldn’t be used as a means to hide bad code from the source. This includes using JavaScript as a way of hiding both invalid or non-semantic markup. In his re-design of Vivabit I think Patrick Griffiths made the right decision in not using JavaScript to add in the extra markup he needed to make his elastic design with all of it’s rounded corners work.

As an alternative example the octopus engine script uses 9 divs to give the visual effect of the rounded corners and drop shadows. Whilst it’s visually appealing, is this the best use of scripting? Does the fact that the ‘real’ markup is clean mean it’s ok to add a ton of extra markup through JavaScript for CSS hooks?

Using JavaScript to add invalid markup

Another thing to avoid would be using JavaScript to add invalid markup so that your site still passes validation. Developers can use this cheat as it’s only the actual source and not the rendered source that is validated. Examples of this would be using JavaScript to write flash objects into a page or any kind of proprietary code just so that your site validates.

My view is, if you don’t feel comfortable putting it in your markup then don’t do it with JavaScript. What’s your point of view?

NB: If you’re reading this and you don’t know how to view source as rendered by the browser including any changes JavaScript made to the document, then the following two Firefox extensions will come in very handy.

Post Tools

Comments: Add yours

1. On March 31st, 2006 at 8:06 am Tim said:

Do you really think what the Octopus Engine deposits on your page is “visually appealing”?

2. On March 31st, 2006 at 8:11 am Stuart Colville said:

As for the Octopus engine being visually appealing, that’s subjective. I was being positive :-)

3. On March 31st, 2006 at 8:36 am Tim said:

Note: You can also use “View Selection Source” in Firefox when drag-selecting content in the browser window.

For some reason, this shows rendered source as well - which kind of does away with the need for the extensions (although they’re handy for other stuff)!

4. On March 31st, 2006 at 8:51 am Stuart Colville said:

Yes “View Selection Source” (Found in the right click (hold down Ctrl on Mac) context menu) does work for rendered source but it very much depends on your selection.

For example if you go to the Octopus engine example and select the text from only one of the boxes it will not show the extra div’s that are added by javascript. This kind of makes sense as you can’t select anything that isn’t text. If you select more than the text of one box you will start to see the rendered source.

5. On March 31st, 2006 at 1:59 pm Andrew said:

I didn’t realize that “View Selection Source” did that. Very handy!

6. On April 4th, 2006 at 7:05 am Mike Ebert said:

I wouldn’t totally condemn adding source code through Javascript–in fact, it’s a rather ingenious way to get the appearance you want out of your site while we all wait for browsers and standards to catch up. One day you won’t need 4 to 8 extra divs to get pretty rounded corners. And when that day arrives, you simply remove the extra code from your JS.

I do agree, however, that there are many instances when this method can be abused, such as including invalid code.

So including valid, but not semantically necessary, markup is ok in my book, but including invalid markup is no better than putting it right in the source.

7. On April 4th, 2006 at 8:07 am Stuart Colville said:

@Mike: I’ll agree to disagree with you on your first point ;-). It’s still good to hear differing views on this subject.

For me I can’t see why anyone would decide to use JavaScript to obscure non-semantic markup. If you make the design decision to add non-semantic markup for whatever reason then you should stand by it and not need to hide it away.

8. On April 5th, 2006 at 1:28 pm Jonathan Snook said:

There’s a couple reasons why I can think of to use Javascript for rounded corners: A <10k script may be far more efficient than the images, HTML and CSS required to pull off multiple rounded corners. Yet you’d still have the benefits of a really clean source for redesigns.

There’s no hard-and-fast rules on this. The important lesson is to evaluate the best solution for the situation.

9. On April 14th, 2006 at 5:49 pm Paul said:

There may be other non-markup related reasons to use javascript to write Flash objects to a page - for example, doing javascript detection to check the browser has Flash installed and substitute an image (or something) if they don’t.

10. On April 15th, 2006 at 2:01 am Stuart Colville said:

@Paul and Jonathan: You are both right and make good points. The point I was making was that if the sole purpose of using javascript is to sweep either non-semantic or invalid markup under the carpet then this is wrong.

I do accept that there are justified reasons for using JavaScript in this way if it provides additional benefits or functionality.

11. On June 14th, 2006 at 8:16 am pavan said:

one can hide their content using java script,but this method should be avoid because your web site may get penalty.

12. On October 7th, 2007 at 12:18 am Kyle said:

I’m making this post a year and a half after the original, but.. I completely disagree with this article. What is the purpose of semantic code and validation? For accessibility and backward compatibility.

It makes perfect sense to add extra features with JavaScript, whether or not it’s “valid markup,” if it will gracefully be disregarded on a non-compliant browser. If, shedding the JavaScript, the markup that will be rendered is semantic and contains all the necessary content for the user of a screen reader, mobile browser, or Lynx browser to get the info they need… by all means, make it cooler for people using browsers that can handle the JS!

13. On October 7th, 2007 at 11:35 am Stuart Colville said:

@Kyle: Thanks for your comment but you’ve totally missed the point. I’m totally in favour of using javascript to enhance a page and I’m also well aware of the need to provide something that works for when JavaScript is available to the UA and when it isn’t.

What I am against is adding mark-up via JavaScript when the only point is to hide the markup that is just there to provide hooks for an effect. It’s totally pointless and that markup may as well be there in the first place. Also using spans which are sematically transparent you are not affecting the overall semantics of a document anyway.







XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Standalone mac battery charger|(0)

Got a spare mac battery? I’ve often wondered why up until now no-one’s produced a standalone charger so that you can charge batteries without having to plug them into the mac. Fortunately Fastmac.com have produced a standalone charger that allows you to do just that. and it’s compatible with iBooks, Powerbooks, macbooks and Macbook Pros. It’s also 110/200v. Exactly what I was looking for!

Django Admin Ominigraffle Stencil|(0)

Colleague Alex Lee has created a nice stencil for omingraffle with the Django Admin UI components, perfect for wireframing customised admin screens. For more details and to download the stencil see Alex’s Blog csensedesign.co.uk

Photos on Flickr

© Copyright 2004-08 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by 1&1 This page was baked in 1.014s.