•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 425,950 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,586 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1965 | Replies: 6
![]() |
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.
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.
Cheers,
Lee-Pro
Lee-Pro
•
•
•
•
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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
> 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.
>
IE doesn't like
> 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. I don't accept change. I don't deserve to live.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Happiness corrupts people.
Failing to value the lives of others cheapens your own.
Hey, thanks for the XMLtoDom converter, but when I tried using it I got this:
The source is here:
Is anything wrong?
Error: [Exception... "String contains an invalid character" code: "5" nsresult: "0x80530005 (NS_ERROR_DOM_INVALID_CHARACTER_ERR)" location: "file:///home/leezh/test.html Line: 57"] Source File: file:///home/leezh/test.html Line: 57
The source is here:
html Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> </head> <body> <script type="text/javascript"> var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { var doc = xmlHttp.responseXML; // Some XML DOM stuff document.getElementsByTagName("body").appendChild(cloneXMLtoDOM(doc)); } } xmlHttp.open("GET","books.xml",true); xmlHttp.send(null); /** * 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; } </script> </body> </html>
Is anything wrong?
Cheers,
Lee-Pro
Lee-Pro
Could you post the XML response from the server.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
There was a problem in the function cloneXMLtoDOM's cloning of attributes. Here's it again, fixed:
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
/**
* 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
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Hey, guys, I found this solution to the problem while searching about:
http://www.captain.at/howto-javascri...-to-string.php
The problem was so simple with just one line of code needed:
But thanks for the help anyways.
http://www.captain.at/howto-javascri...-to-string.php
The problem was so simple with just one line of code needed:
javascript Syntax (Toggle Plain Text)
var string = (new XMLSerializer()).serializeToString(xmlobject);
But thanks for the help anyways.
Cheers,
Lee-Pro
Lee-Pro
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
ajax asp cross-browser javascript menu with few lines of code developer development firefox home html internet javascript javascript smooth scrolling scroll smoothly window document position javascript tab menu with rounded corners generator microsoft msdn office prevent javascript menu from getting hidden under flash movies site software sql vista web
- AJAX resources (JavaScript / DHTML / AJAX)
- Ajax Question (ASP.NET)
- Levyo Ajax Technology Gallery (JavaScript / DHTML / AJAX)
- AJAX : Am i right with the concept? (JavaScript / DHTML / AJAX)
- AJAX and Smarty (PHP)
- AJAX techniques... (JavaScript / DHTML / AJAX)
- newbie question about vbulletin (Growing an Online Community)
- AJAX in Apache? (Linux Servers and Apache)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: to select a string from textarea
- Next Thread: Can I control the Screen resolution in web page?



Linear Mode