...

View Full Version : Mozilla deals with DOMs differently



deadseasquirrel
06-11-2004, 10:09 PM
First I am going to say this is not a repost, even though the code is similar to my other posts. My question is slightly different now.

So the state of my code to load a xml data island into a DOM, after many many iterations is this:


function loadXML(){
try {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.onreadystatechange = verify;
hasFile = xmlDoc.load(info.XMLDocument);
if (hasFile){
xmlObj = xmlDoc.documentElement; // xmlObj is pointing to <test> at this point
allTopics = xmlObj.getElementsByTagName("topic");
}
}
catch(e) {
xmlObj = document.getElementsByTagName("xml")[0]; // xmlObj is pointing to <test> at this point
xmlDoc = (new DOMParser()).parseFromString(xmlObj.innerHTML, 'text/xml');
hasFile = true;

if (hasFile){
allTopics = xmlDoc.getElementsByTagName("topic");
}
}
}

The try is for IE, the catch is for Mozilla, and my xml is something like <xml id="info>...</xml>

The question i have is if Mozilla can ever have a handle on a DOM the same way that IE does. What happened with me was that i built a page entirely for IE, and then decided I should rebuild the page for cross-platform compliance. I thought that as long as if rebuilt the loadXML() function and had all the right objects from the start, then everything else is the same for both browsers. But I'm finding out that that doesn't seem to be the case.

For example if I wanted to see what was in my xmlObj, I could write alert(xmlObj.xml) in IE, but in mozilla, I have to do (xmlObj.innerHTML). You would think they are same thing but they aren't. So I guess my question is, am I doing it wrong, or is this just the way DOMs are handled in both situations?

neofibril
06-13-2004, 05:27 PM
A Cross-Browser DOM Document Wrapper (http://www.webreference.com/programming/javascript/domwrapper/)

jkd
06-13-2004, 10:06 PM
Well, the deal is <xml> is a proprietary tag. Mozilla does not have a specific interface to deal with it, instead it applies a generic HTMLUnknownElement interface to it which only inherits from HTMLElement and doesn't apply anything else. .xml is a property that IE specifies for this particular tag, but since Mozilla doesn't support this tag (nor should it), it doesn't apply any extra properties to it. What you could do is something like:



function HTMLXmlElement() {}
HTMLXmlElement.prototype.getters = [];
HTMLXmlElement.prototype.setters = [];

HTMLXmlElement.prototype.getters["xml"] = function() {
return this.innerHTML;
};
// emulate any more necessary properties


HTMLUnknownElement.prototype.inheritFrom = function(myInterface) {
for (var i in myInterface.prototype) {
switch (i) {
case "getters":
for (var j in myInterface.prototype.getters)
this.__defineGetter__(j, myInterface.prototype.getters[j]);
break;
case "setters":
for (var j in myInterface.prototype.setters)
this.__defineSetter__(j, myInterface.prototype.setters[j]);
break;
default:
this[i] = myInterface.prototype[i];
}
}
}


And then:

document.getElementById("myXMLElementID").inheritFrom(HTMLXmlElement);

and bam, you'll have the .xml property, and whatever else you chose to implement.

deadseasquirrel
06-14-2004, 03:46 PM
I'll implement that the next chance I get. But hte question isn't so much as to how the XML is handled as a data island - I don't expect any browser to treat <xml> as anything other than an unrecognizable tag. The real question is whether I can strip the information from the <xml></xml> tags and put it in a usable form such as a DOM object. It seems like IE gives me a really good way of pushing data islands into a DOM, whereas mozilla can parse the <xml></xml> data out, but I can't seem to put it in a similiarly well-formed DOM, not with parseFromString(), not with document.implement.create(). I mean my big issue isn't even with getting the information out of the <xml></xml> it is with this line:

xmlDoc = (new DOMParser()).parseFromString(xmlObj.innerHTML, 'text/xml');

It simply doesn't create a DOM like IE's:
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.load(info.XMLDocument);

meaning xmlDoc in one version is not the same as xmlDoc in the other version when you come out of the tunnel from the other side. I haven't read the cross-browser DOM wrapper article yet. But I'll let you know if it answers my questions. Thanks

jkd
06-14-2004, 05:45 PM
What exactly is the DOMParser() returning then? I'd expect only whitespace nodes to be different in the resulting DOM, which you would get anyway with Mozilla, since it preserves them while IE parses them out. But the root element should still be the same, and all the same elements should exist in the DOM hierchy.

deadseasquirrel
06-14-2004, 06:12 PM
Well I don't know exactly, I'm trying to read as much as I can, and understand at least half of it. All I know is that at the end of the day if you look at my code above allTopics does not point to the same exact thing because I tried to use it in my code and it works in IE but in Mozilla it doesn't, and the only difference in the paths they take is that IE goes in the try{} statement and Mozilla goes in the catch{} statement. I am still reading that article above which I think is useful, but it will take me a while to digest everything.

jkd
06-14-2004, 06:18 PM
Could you perhaps put a working example online somewhere?

nolachrymose
06-14-2004, 07:29 PM
What exactly is the DOMParser() returning then?

DOMParser.prototype.parseFromString() returns an XMLDocument.

Happy coding! :)

deadseasquirrel
06-14-2004, 07:29 PM
Here is the file that I've been working on. Everything should be in the html. The css is in there, the <xml> should be in there. It works in IE (6.0 atleast) but not in mozilla). Hope this is helpful.

jkd
06-14-2004, 08:10 PM
DOMParser.prototype.parseFromString() returns an XMLDocument.

Happy coding! :)
I know that... was asking what the document it was returning in this instance looked like. Ah well.

jkd
06-14-2004, 08:17 PM
Running your code through a few tests... I don't see any of the issues you're talking about? In Firefox 0.8, xmlDoc is [object XMLDocument], xmlDoc.documentElement is [object Element] with nodeName "test". And getElementsByTagName("topic").length is 4 on each of them.

I did have to copy/paste some of the script from this thread into the page though, as the Mozilla portion was still trying to load from loader.xml instead of from the embedded XML tag.

deadseasquirrel
06-14-2004, 08:47 PM
So are you saying the script actually worked in FireFox and mozilla because it doesn't work for me (the bottom line). If it worked for you, do you mind sending it to mui_wilson@hotmail.com and let me take a look, with the changes you said you copy and pasted too?

I mean the lengths might be right, but then accessing allTopics, as I recall, was still not exactly the same between both versions.

jkd
06-14-2004, 08:54 PM
var xmlDoc;
var allTopics;

/* creates XML DOM object */
function loadXML(){
if (window.DOMParser)
{
xmlDoc = (new DOMParser()).parseFromString(document.getElementsByTagName("xml")[0].innerHTML, 'text/xml');
hasFile = true;
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.onreadystatechange = verify;
hasFile = xmlDoc.load(info.XMLDocument);
}
else
{
alert('Your browser can\'t handle this script');
return;
}


if (hasFile) {
allTopics = xmlDoc.getElementsByTagName("topic");
}
}

deadseasquirrel
06-14-2004, 10:55 PM
I tried that modification to my code, but the javascript as a whole still doesn't work. It works in IE, and what it is suppose to do is, everytime you click on those menus on top, the content is suppose to change on the bottom. It does it in IE, but nothing happens in Mozilla. Except for the error "xmlPoint.getAttribute is not a function"

jkd
06-15-2004, 02:41 AM
xmlPoint? Where is that defined...?

deadseasquirrel
06-15-2004, 04:40 PM
I'm assuming you are looking at my .zip file. xmlPoint is defined in displayContent(){} right before the first while loop. It is defined as xmlTopic.firstChild, and xmlTopic = allTopics[i], where 'i' is the index of the specific topic node that corresponds to the top selection bar: Sessions, Materials and Resources, etc.

Is that understandable?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum