views:

53

answers:

3

Hi there. I'm currently trying to write some javascript to loop through the elements in a table, but was wondering what the best practise was. I could just call .cells[] on my table object to get all of the cells in the table, but the W3Schools page says that this is not a W3C standard - should I avoid it then?

The other option is to use .rows[] to get all the rows (which is W3C Standard), then .cells[] on each of the rows (again, W3C Standard). Basically - how important is it that I stick to W3C Standard methods?

+2  A: 

It is of the utmost importance to use standard APIs and properties as defined by the W3C. This ensures cross-browser compatibility, future-proofing and consistency.

Delan Azabani
How do you feel about `innerHTML`?
Tim Down
I read this as sarcasm.
banzaimonkey
banzaimonkey: Delan's answer or my comment?
Tim Down
@Tim Down — While I try to avoid it (for a variety of reasons), it is part of a draft standard: http://www.w3.org/TR/html5/apis-in-html-documents.html#innerhtml
David Dorward
Tim, strangely enough, I'm actually OK with innerHTML, as its support is ubiquitous.
Delan Azabani
David Dorward: yes, but as I'm sure you well know, that is a comparatively recent development. `innerHTML` is a prime example of something most people find extremely useful and existed with near universal browser support for many years without being part of any standard, while a well-supported but more unwieldy standard for doing the same thing already existed (DOM).
Tim Down
@Tim Down Delan's answer, for myriad reasons I think are obvious to someone who's been using html, css, and javascript for awhile.
banzaimonkey
banzaimonkey: I read it as sincere. That would've been a seriously dry bit of sarcasm.
Tim Down
@Delan — except for the many oddities it has in IE :)
David Dorward
... or the many oddities it has in other browsers. IE's version was there first.
Tim Down
A: 

In most cases its not important what the w3c says but if all your supported browser can handle the feature. For cells it should be no problem: http://quirksmode.org/dom/w3c_html.html#t33.

eskimoblood
Actually that quirksmode page is misleading: the example it uses is the `cells` property of a table row rather than of a table. In fact, the `cells` property of a table is not supported in Firefox (and possibly other browsers).
Tim Down
I completely disagree with “[i]n most cases its not important what the w3c says”: if web developers do not (try to) adhere to the standards, browser vendors will not be encouraged to improve their products to make cross-browser development easier.
Marcel Korpel
+2  A: 

Where the standard exists, it's generally preferable to use it: there's more chance that it's well-supported in all your target browsers and wide adoption of standards helps prevent the proliferation of non-standard proprietary APIs.

This particular case is a no-brainer: the cells property of a <table> element isn't supported in Firefox, so you can't use it on the web.

Tim Down