I’m having some trouble using the responseXML property.
This function tries to load an external XHTML file and copy the contents of its body (all the child nodes and their descendants) into a content div in the main page (identified by pDestId):
[code]function pLoad(pDestId, pSrcFileName)
{
var request = false; // HTTP request object
var destNode;
var srcNode;
// Try creating HTTP request object
try { request = new XMLHttpRequest(); } // conforming browsers
catch (e)
{
try { request = new ActiveXObject(“Msxml2.XMLHTTP”); } // MSIE
catch (e)
{
try { request = new ActiveXObject(“Microsoft.XMLHTTP”); }
catch (e)
{
alert(“Error: HTTP request object could not be created. Your browser must not support AJAX.”);
return false;
}
}
}
// Open file
request.open(“GET”, pSrcFileName, true);
request.send(null);
// Set state change method
request.onreadystatechange = function()
{
if ( request.readyState == 4 &&
request.status == 200 )
{
// Replace content
[b][i]srcNode = document.importNode(request.responseXML.documentElement.getElementsByTagName(“body”)[0], true);[/i][/b]
destNode = document.getElementById(pDestId);
removeChildren(destNode);
copyChildren(destNode, srcNode);
}
}
}
The above code works fine when I try it locally (in every browser but IE), but when I upload it to a server it doesn’t work at all. The problem seems to be only with the bold line above.
Everything works perfectly if I replace it with something like this (so, creating my own node tree instead of reading it from a file):
[code]…
{
// Replace content
[b][i]srcNode = document.createElement(“div”);
srcNode.appendChild(document.createTextNode(“stuff”));[/i][/b]
destNode = document.getElementById(pDestId);
removeChildren(destNode);
copyChildren(destNode, srcNode);
}
…
So it seems like it’s a problem with the “responseXML” line.
Also, my XHTML is being served as “text/html”, so could that be causing this problem?
I’ve got an equivalent function which uses innerHTML, and it works in every browser I’ve tested it in. I’m just trying to write something using standard DOM methods instead.