Results 1 to 3 of 3

Thread: Loading XML with JS only working in IE

  1. #1
    Join Date
    Aug 2007
    Location
    Belgium
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Loading XML with JS only working in IE

    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.
    Last edited by Chinmayo; 08-06-2007 at 04:11 PM.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Check if relatedThought is a text node.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Aug 2007
    Location
    Belgium
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    Already 'MainThought' is not loaded (it gives 'undefined'), so I think the mistake lies earlier in the script...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •