The Feminine Wiles of innerHTML
Jeremy Keith has been keeping a promotional weblog for his (by all accounts excellent) new book DOM Scripting: Web Design with JavaScript and the Document Object Model. Earlier this month he posted on something that’s been bugging a lot of developers who try to grapple with that newfangled beast known as AJAX — whether it’s acceptable to use innerHTML as a less-semantic, less-abstracted, non-DOM method for plugging the results of a JavaScript remote data call into a web page.
Both sides of the debate argue passionately in the comments on Jeremy’s post, and they both make good points. It was in the midst of all this rhetorical jousting that Jonathan Snook dropped a real gem:
I think innerHTML is sexy. It’s so easy as to be sleazy.
Oh, great. Now innerHTML will be forever personified in my mind as a smirking, raven-haired seductress in a dress cut down to here and slit up to there, throwing herself at me despite my best efforts to walk the straight and narrow of semantically correct code. How shall I ever resist her wanton advances??
… I think I may need to get out more.
Ahem. Anyway, the debate breaks down like this: Critics say that using innerHTML is sloppy and evil, courting everything from semantically invalid markup to the downfall of the free world. Pragmatists, in turn, note that it may be sloppy but it’s a hell of a lot faster to slap some generic HTML into a page element with one line of code using innerHTML than to write twenty lines of createElement / createTextNode / appendChild nonsense to do the same thing with “correct” DOM methods. As Jeremy notes, there’s even a new microformat dubbed AHAH to make the use of innerHTML even easier. The real punchline here is that XMLHttpRequest isn’t part of the DOM either, and innerHTML actually enjoys better browser support.
My take is this: Neither method is perfect, and these technologies simply aren’t mature enough yet to claim that anything is truly the “standard” for getting the job done. If you’re dealing with XML as a back-end data store, handling business logic or order processing rather than simple page updates, or already using XSLT then the DOM methodology may very well be a safer bet. If not, then go ahead and save yourself some time and trouble with innerHTML. I’m just barely getting my toes wet with this stuff and could be way off base, but that’s the way I see it.
Updates:
- 12/19/2005 @ 12:17 AM — For some informative reviews of DOM Scripting, check this Google search. It leaves out the major book sellers’ web sites, the promotional blog, and the author’s personal web site. Among what remains, the top hits mainly seem to be reviews by web developers, posted to their own blogs and journals.
- 12/19/2005 @ 11:58 AM — Microformats.org has some more info on AHAH and how it compares to traditional AJAX.

Technorati Tags for This Post

[…] The Feminine Wiles of innerHTML (8 Ways to Sunday): […]
Pingback by microformats | weblog | AJAX vs. AHAH — January 17, 2006 @ 3:02 pm