I've been following Uche Ogbuji's Javascript in Firefox series on Developerworks. It sounds like e4x will be in the next article. I can't wait. Here's the javascript form the "labels" example using e4x instead of DOM. Compare this javascript to Listing 2 in the article:
function loadAddresses(){
var doc = document.implementation.createDocument("", "", null);
doc.onload = function(){writeList(doc);};
doc.load("labels.xml");
}
function writeList(domDoc){
var xdoc = new XML((new XMLSerializer()).serailzeToString(domDoc.documentElement));
var xlist = <ol/>;
for each (label in xdoc..label){
xlist.* += <li>{label.name.text() + ' (' + label.@id + ')'}</li>;
}
document.getElementById('updateTarget').innerHTML = xlist;
}
Isn't this throwing the baby out with the bathwater a little.
function writeList(){
var xdoc = new XML((new XMLSerializer()).serializeToString(xmlDoc.documentElement));
var xlist = ;
for each (label in xdoc..label){
xlist.* += {label.name.text() + ' (' + label.@id + ')'};
}
var domNode = new DOMParser().parseFromString(xlist.toXMLString(),"text/xml").documentElement;
document.getElementById('updateTarget').appendChild(domNode);
}
Will do the same job and not rely on innerHTML.
Posted by: Jeremy French | August 15, 2006 at 06:09 AM
Jeremy, you are correct. I was trying to minimize DOM usage under the assumption that Brendan and his javascript gurus will atually implement the e4x<>DOM integration someday. Then all the serialzing and parsing goes away. But yes, innerHTML is the greater of two evils so a little DOM code to make it go away is a win.
Posted by: Dan Sickles | August 15, 2006 at 10:47 AM