PDA

View Full Version : appendChild implementation doesn't work with w3schools's loadXMLString function



ReMaX
04-24-2008, 03:13 AM
Heyho,

I have a problem: I made a window handler class. It is great and I am very proud of it :cool: but there is the problem that it now runs the document.write function although I'd like to run it in a spontaneous call in the context and not every time at the end of the document. I tried to use that loadXMLString function of the w3c but it doesn't work.
Here's a short example:


function VirtualWindows() {

this.putContent = function(windowcontent) {
var content = this.loadXMLString(windowcontent);
var mainbody = document.getElementsByTagName("body")[0];
mainbody.appendChild(content.documentElement);
};

this.loadXMLString = function(txt) {
try { //Internet Explorer
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(txt);
return(xmlDoc);
}
catch(e) {
try { //Firefox, Mozilla, Opera, etc.
parser = new DOMParser();
xmlDoc = parser.parseFromString(txt,"text/xml");
return(xmlDoc);
}
catch(e) { alert(e.message); }
}
return(null);
};
}

call:


var windowmanager = new VirtualWindows();
windowmanager.putContent('<div class="testmessage">hey, I am a test. What do you are? <div><span>I am a child element that only has one parent</span></div><div></div></div>');

Why doesn't this work? If I try alert(content.documentElement.nodeName); at the end of the putContent() function "div" appears but I cannot get the class, there is no possibility to implement it in the document and nothing else works.

Please, help!!! :confused:

greetings
Max

ReMaX
04-24-2008, 07:35 PM
Nobody knows an answer? Help!!!
*push*

BYK
04-24-2008, 08:50 PM
Your "putContent" function receives only the content but you are giving "testWindow" as the first parameter and the content as the second parameter.

Why do you do that? :)

ReMaX
04-25-2008, 01:26 AM
ah, excuse me. That should only be an abstraction. I already changed the code afterwards but I forgot to turn the call to a function with one parameter :rolleyes:. Anyway, it doesn't work! Waaaaah

greetings
Max

BYK
04-25-2008, 04:59 AM
Hey, It works with the proper call :)
I mean, it added the div to the bottom of my page when I tried it ;)

ReMaX
04-25-2008, 07:55 PM
Hm, okay, so everything is fine. I think I simply was too tired that morning. Now I made it with element.innerHTML. A few days ago I thought that would be the same as element.data (the first TextNode) but now I know it contains the "innerHTML", all childElements.:D That's how it works, too!
Excuse me for wasting your time.

greetings
Max