| | |
AJAX Question
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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,
LeeZH
LeeZH
•
•
•
•
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.
Traverse each node in the XML Doc and create equivalent DOM nodes in your HTML Document.
Example:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
/** * 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.
>
> 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.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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.
Hey, thanks for the XMLtoDom converter, but when I tried using it I got this:
The source is here:
Is anything wrong?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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,
LeeZH
LeeZH
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
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
/** * 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,
LeeZH
LeeZH
![]() |
Similar Threads
- 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: Disabling back button in asp.net
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxjspservlets automatically beta box browser bug captchaformproblem checkbox close codes createrange() css cursor debugger decimal dependent disablefirebug dom download dropdown editor element engine enter error events explorer ext file firefox form forms frameworks getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe index internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump listbox maps masterpage math media menu microsoft mp4 object onmouseoutdivproblem onreadystatechange paypal pdf php player position problem programming progressbar prototype redirect regex runtime safari scale scriptlets search security select size software sql text textarea unicode w3c window windowofwords windowsxp wysiwyg \n






