PDA

View Full Version : Loading XML with JS only working in IE



Chinmayo
08-06-2007, 09:18 AM
I have a problem with loading an XML file into HTML with JS. It works fine in IE, but not in other browsers. Firefox gives the following error message:
Error: relatedthought.getAttribute is not a function (in line 52 of the script).
URL: http://www.pantha.net/Old/JSBrain/

Here is the script:

function popUp(page){
window.open(page,'','menubar=1,directories=1,toolbar=1,scrollbars=1,location=1,status=1,resizable=1,width=700,height=500,left=200,top=180,screenX=200,screenY=18 0');
}
xmlFileName = "pantha.xml"
if (window.ActiveXObject){
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
}
else if (document.implementation && document.implementation.createDocument)
var xmlDoc= document.implementation.createDocument("","doc",null);
if (typeof xmlDoc!="undefined")
xmlDoc.load(xmlFileName);
var notWhitespace = /\S/
function quest(){
var msgobj=xmlDoc.getElementsByTagName("thought")[0]
for (i=0;i<msgobj.childNodes.length;i++){
if ((msgobj.childNodes[i].nodeType == 3) && (!notWhitespace.test(msgobj.childNodes[i].nodeValue))) {
msgobj.removeChild(msgobj.childNodes[i])
i--
}
}
var myID = 0;
var StringA=document.URL;
var LengthA=StringA.length
var A=StringA.lastIndexOf("#")+1;
var ThisAnchor = ''
if (A == 0) {
}
else {
ThisAnchor=StringA.substring(A,LengthA);
}
if (ThisAnchor.length == 0){
myID = 1;
}
else {
myID = ThisAnchor;
}
var Thoughts = xmlDoc.getElementsByTagName("thought");
for (i=0; i<Thoughts.length; i++){
if (Thoughts.item(i).getAttribute("id") == myID){
var MainThought = Thoughts.item(i)
}
}
var NameOfMainThought = MainThought.getElementsByTagName("name").item(0).text
if (MainThought.getElementsByTagName("location").length>0){
var LocationOfMainThought = MainThought.getElementsByTagName("location").item(0).text
document.getElementById("main").innerHTML='<a href="#" onclick="popUp(\'' + LocationOfMainThought + '\')">' + NameOfMainThought +'</a>'
}
else {
document.getElementById("main").innerHTML= NameOfMainThought
}
var RelatedThoughts = MainThought.getElementsByTagName("relations").item(0).childNodes;
var childrenHtml = "";
var jumpsHtml = "";
var parentsHtml = "";
for (i=0; i<RelatedThoughts.length; i++){
var relatedthought = RelatedThoughts.item(i)
var IdOfRelatedThought=relatedthought.getAttribute('id')
if (relatedthought.childNodes && relatedthought.childNodes.length>0){
var NameOfRelatedThought=relatedthought.getElementsByTagName("name").item(0).text;
}
else{
for (j=0; j<Thoughts.length; j++){
if (Thoughts.item(j).getAttribute("id") == IdOfRelatedThought){
var NameOfRelatedThought=Thoughts.item(j).getElementsByTagName("name").item(0).text;
}
}
}
var HrefOfRelatedThought = '<a href="index.html#'+ IdOfRelatedThought + '" onclick="window.location.reload()">' + NameOfRelatedThought + '</a>'
if (relatedthought.nodeName == "child") childrenHtml+=HrefOfRelatedThought + '<br />';
if (relatedthought.nodeName == "jump") jumpsHtml+=HrefOfRelatedThought + '<br />';
if (relatedthought.nodeName == "parent") parentsHtml+=HrefOfRelatedThought + '<br />';
}
document.getElementById("children").innerHTML=childrenHtml;
document.getElementById("jumps").innerHTML=jumpsHtml;
document.getElementById("parents").innerHTML=parentsHtml;
}

if (typeof xmlDoc!="undefined"){
if (window.ActiveXObject)
quest()
else
xmlDoc.onload=quest
}

Much obliged if anyone can help me.

Twey
08-06-2007, 04:22 PM
Check if relatedThought is a text node.

Chinmayo
08-07-2007, 02:56 AM
Already 'MainThought' is not loaded (it gives 'undefined'), so I think the mistake lies earlier in the script...