I send an XmlHttpRequest and GET a response back. Is it possible to
convert that response into a DOM Level 3 HTML 'document' object and
investigate it?
Collecting different pieces of code for a while, removing good from
bad etc.,
now I use the following function which I hope (and think) you'll find
of use. Just
hand it the ajax object (after it had sent the request and the
readyState had been
4), and you'll be returned the document. If I copied it correctly,
then it will work like
it does on my site, and that is - supporting Opera, IE and Mozilla.
function getXMLDocument( ajax )
{
if (typeof DOMParser == "undefined") {
DOMParser = function()
{};
DOMParser.prototype.parseFromString = function(str, contentType)
{
if (typeof ActiveXObject != "undefined") {
var doc = new ActiveXObject("MSXML.DomDocument");
doc.loadXML(str);
return doc;
} else if ( typeof XMLHttpRequest != "undefined" ) {
var req = new XMLHttpRequest();
req.open("GET", "data:" + (contentType || "application/xml") +
";charset=utf-8," + encodeURIComponent(str), false);
if ( req.overrideMimeType )
req.overrideMimeType(contentType);
req.send(null);
return req.responseXML;
} else
throw new FatalException( "Can't find a valid xml parser",
"AJAX::getXMLDocument()" );
}
}
var strDocument = ajax.responseText;
var xmlDocument = ajax.responseXML;
try {
if( ! xmlDocument || xmlDocument.childNodes.length === 0 )
xmlDocument = (new DOMParser()).parseFromString( strDocument,
"application/xml" );
return xmlDocument;
} catch( e ) {
return null;
}
}
Regards