[thelist] AJAX Tip

Christian Heilmann codepo8 at gmail.com
Thu Apr 6 09:11:05 CDT 2006


> > Is the innerHTML function of a document object not part of the "Document
> > Object Model"?
>
> No it isn't. It's originally a Microsoft extension, but nowadays many
> browsers support it. Purists frown on it, but it is often faster than
> the DOM way.

Arguably. It is dead handy for debugging and also for writing out a
bunch of HTML you got from  an XHR. For generating HTML it is not as
clever though, as you don't leave a trail of developed objects behind
and need to reach them again:

va bla=document.getElementById('specialLink');
bla.innerHTML='<p><a href="#">retrieve specials</a></p>';
var speciallink=bla.getElementsByTagName('a')[0];
addEvent(speciallink,'click',foo,false);

vs.

var bla=document.getElementById('specialLink');
var p=document.createElement('p');
var specialLink=document.createElement('a');
specialLink.setAttribute('href','#');
addEvent(specialLink,'click',foo,false);
p.appendChild(specialLink)
bla.appendChild(p);

If another function needs to reach that A it'll always have to do a
getElementsByTagName in the innerHTML example, in the DOM example it
can just change specialLink.

This also promotes the idea of HTML as a node tree, and not as a huge string.

--
Chris Heilmann
Blog: http://www.wait-till-i.com
Writing: http://icant.co.uk/
Binaries: http://www.onlinetools.org/



More information about the thelist mailing list