Handling XML Data for Ajax - XML Parsing with JavaScript
(Page 3 of 5 )
Remember, it all starts with our callback() function in JavaScript:
function callback() {
if (req.readyState==4) {
if (req.status == 200) {
if (window.XMLHttpRequest) {
nonMSPopulate();
}
else if (window.ActiveXObject) {
msPopulate();
}
}
clear();
}
}
Remember howconvertToXML()had to determine whether the browser was Internet Explorer or something else? We have the same problem again here. Whencallback()is invoked, it must check to see whether we are runningActiveXObject(Internet Explorer) orXMLHttpRequest(all other major browsers).
If the browser is Internet Explorer, we runmsPopulate()to strip out the data from the XML. Otherwise, we runnonMSPopulate(). What's the difference? It has to do with how we get an XML parser and with the API that parser presents to us. Firefox, Mozilla, and Safari all usenew DOMParser()to get a built-in parser that can parse XML, and it's rumored that Opera will support this soon. Internet Explorer, on the other hand, usesnew ActiveXObject("Microsoft.XMLDOM")to get the Microsoft XML parser.
Although Ajax works on most browsers in their current released states, it's entirely fair to say that the problem of cross-browser compatibility is the Achilles' heel of web applications.
Next: Populating the Form on a Microsoft Browser >>
More XML Tutorials Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Ajax on Java, written by Steven Douglas Olson (O'Reilly, 2007; ISBN: 0596101872). Check it out today at your favorite bookstore. Buy this book now.
|
|