Hey everyone, this is a very tough problem I am facing right now in JavaScript.
I'm designing this script to load a page like an IFrame but is using AJAX, the only problem is that I need to edit certain attributes before displaying them through changing an element's innerHTML attribute.
Using the normal xmlHttp request, it retrieves a html document. But is there any way I could actually handle it through some kind of DOM before actually displaying it?
Modifying them after I display them is troublesome as the browser would convert relative links into absolute paths.
Using XML to load the document was fine, but then there was no way to generate the HTML code without lots of hassle.
Is there any other way?
PS: An older version of script is in my site, but it uses substr which is very messy.
You could load the HTML with an XML content-type so that you can access the response as XML (XMLHttpRequest.responseXML).
Traverse each node in the XML Doc and create equivalent DOM nodes in your HTML Document.
Example:
/**
* Clones XML nodes to the current Document's DOM
*/
function cloneXMLtoDOM(Node) {
var DOMNode;
if (Node.nodeName == '#text') {
// clone text nodes
DOMNode = document.createTextNode(Node.data);
} else {
// clone root node
DOMNode = document.createElement(Node.nodeName);
// clone the attributes
for(var i = 0; i < Node.attributes.length; i++) {
DOMnode.setAttribute(Node.attributes[i].nodeName, Node.attributes[i]);
}
// recursively clone the child nodes
if (Node.hasChildNodes()) {
for(var i = 0; i < Node.childNodes.length; i++) {
DOMNode.appendChild(cloneXMLtoDOM(Node.childNodes[i]));
}
}
}
return DOMNode;
}
You could also use XPATH which would be faster.
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
> Using the normal xmlHttp request, it retrieves a html document. But is there any way I could
> actually handle it through some kind of DOM before actually displaying it?
Make sure that the page you fetch using XHR is well formed. If that is the case, you can manipulate the contents of the response just as you would to a normal HTML document.
var doc = xmlHttpRequest.responseXML;
doc.getElementsByTagName("someTag")[0].someAttribute = someValue;
> DOMnode.setAttribute(Node.attributes[i].nodeName, Node.attributes[i]);
IE doesn't like set/getAttribute . Use obj.attribute = value; for maximum browser compatibility.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Could you post the XML response from the server.
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
There was a problem in the function cloneXMLtoDOM's cloning of attributes. Here's it again, fixed:
/**
* Clones XML nodes to the current Document's DOM
*/
function cloneXMLtoDOM(Node) {
var DOMNode;
if (!Node) return false;
if (Node.nodeName == '#text') {
// clone text nodes
DOMNode = document.createTextNode(Node.nodeValue);
} else {
// clone root node
DOMNode = document.createElement(Node.nodeName);
// clone the attributes
for(var i = 0; i < Node.attributes.length; i++) {
DOMNode.setAttribute(Node.attributes[i].nodeName, Node.attributes[i].nodeValue);
}
// recursively clone the child nodes
if (Node.hasChildNodes()) {
for(var i = 0; i < Node.childNodes.length; i++) {
DOMNode.appendChild(cloneXMLtoDOM(Node.childNodes[i]));
}
}
}
return DOMNode;
}
If it doesn't work for you, just use the DOM methods on the responseXML to make modifications and append it to the document using innerHTML like s.o.s mentioned
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101