www.webdeveloper.com
Results 1 to 5 of 5

Thread: DOM Internet Explorer Error

  1. #1
    Join Date
    Jun 2009
    Posts
    9

    Post DOM Internet Explorer Error

    I wrote this simple DOM script that pulls the data off of an XML atom feed and displays it. The script works great in everything except for IE 5.5 and 6 which get an error and refuse to load it. How can I get IE 5.5 or 6 to load this script or make it work? Here is a live example too.

    main code:
    <html>
    <head>
    <script type="text/javascript" src="javascript/loadxmldoc.js"></script>
    <script type="text/javascript" src="javascript/atomnewsfeed.js"></script>
    </head>
    <body>

    <script type="text/javascript">
    // Load XML document
    xmlDoc=loadXMLDoc("atom.xml");
    atomnewsfeed ();
    </script>
    </body>
    </html>


    loadxmldoc.js
    function loadXMLDoc(dname)
    {
    var xmlDoc;
    if (window.XMLHttpRequest)
    {
    xmlDoc=new window.XMLHttpRequest();
    xmlDoc.open("GET",dname,false);
    xmlDoc.send("");
    return xmlDoc.responseXML;
    }
    // IE 5 and IE 6
    else if (ActiveXObject("Microsoft.XMLDOM"))
    {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.load(dname);
    return xmlDoc;
    }
    alert("Error loading document");
    return null;
    }

    atomnewsfeed.js
    function atomnewsfeed () {
    // Set variable to 0 and begin blog counting
    var blogEntry = 0;
    if (blogEntry < 3)
    var i = blogEntry;
    while (i < 3) {
    // Grab blog values
    title=xmlDoc.getElementsByTagName("entry")[i].getElementsByTagName("title")[0].childNodes[0];
    date=xmlDoc.getElementsByTagName("entry")[i].getElementsByTagName("published")[0].childNodes[0];
    author=xmlDoc.getElementsByTagName("entry")[i].getElementsByTagName("name")[0].childNodes[0];
    summary=xmlDoc.getElementsByTagName("entry")[i].getElementsByTagName("summary")[0].childNodes[0];

    // Display blog values
    document.write("<h1>" + title.nodeValue + "</h1>", "<p>" + date.nodeValue.substr(0, 10) + "</p>", "<p>" + author.nodeValue + "</p>", "<p>" + summary.nodeValue + "</p>");
    i++;
    }
    }

    XML sheet link

  2. #2
    Join Date
    Jun 2009
    Posts
    27
    I use a function to help me build a cross-browser xmlhttp connection. It's pretty flawless:

    Code:
    /** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
     ** Code licensed under Creative Commons Attribution-ShareAlike License      **
     ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
    function XHConn()
    {
      var xmlhttp, bComplete = false;
      try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
      catch (e) { try { xmlhttp = new XMLHttpRequest(); }
      catch (e) { xmlhttp = false; }}}
      if (!xmlhttp) return null;
      this.connect = function(sURL, sMethod, sVars, fnDone)
      {
        if (sVars != '') { sVars = "?"+sVars }
    	if (!xmlhttp) return false;
        bComplete = false;
        sMethod = sMethod.toUpperCase();
    
        try {
          if (sMethod == "GET")
          {
            xmlhttp.open(sMethod, sURL+sVars, true);
            sVars = "";
          }
          else
          {
            xmlhttp.open(sMethod, sURL, true);
            xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
            xmlhttp.setRequestHeader("Content-Type",
              "application/x-www-form-urlencoded");
          }
          xmlhttp.onreadystatechange = function(){
            if (xmlhttp.readyState == 4 && !bComplete)
            {
              bComplete = true;
              fnDone(xmlhttp);
            }};
          xmlhttp.send(sVars);
        }
        catch(z) { return false; }
        return true;
      };
      return this;
    }
    And then, I just use the connection to issue the request and execute code onready:

    Code:
    var myConn = new XHConn();
    	var fnWhenDone = function (oXML) {  
    		document.getElementById(id).innerHTML = oXML.responseText; 
    	};
    	myConn.connect(url, "GET", qstring, fnWhenDone);
    So, if your problem lies in your connection and old-IE is holding you back, try XConn() and see if it fills the gap. If not, your browser's security settings might be preventing the connection.

  3. #3
    Join Date
    Jun 2009
    Posts
    9

    Thumbs up Great idea

    Thanks, this helps a lot. Only problem is I'm confused on how to use this. How would I change my current page the code is being pulled into, to get the xml data sheet pulling correctly (can you give a quick example and thank you so much for what you have helped with). I'm still learning JavaScript so don't laugh if this is way off, but I can't figure out why this wont work. If not what am I doing wrong?

    <html>
    <head>
    <script type="text/javascript" src="javascript/xmlload.js"></script>
    <script type="text/javascript" src="javascript/atomnewsfeed.js"></script>
    </head>
    <body>

    <script type="text/javascript">
    // Load XML document
    var myConn = new XHConn();
    var fnWhenDone = function (oXML) {
    document.getElementById(id).innerHTML = oXML.responseText;
    };
    myConn.connect("atom.xml", "GET", qstring, fnWhenDone);

    // Process and gather XML code
    atomnewsfeed ();
    </script>
    </body>
    </html>
    Last edited by truefreestyle; 06-06-2009 at 04:02 AM.

  4. #4
    Join Date
    Jun 2009
    Posts
    1
    Giving this a bump because I have the same problem, I'm using the same phraser as truefreestyle (I got it from w3schools.com) and I'm not sure how to implement the code sommardahl posted.

    Code:
    function firstLinkarea()
    {
      var xmlDoc=loadXMLDoc("links.xml");
      var txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("url");
      document.write('<a href="');
      document.write(txt);
      document.write('">');
      return ("");
    }
    I'm new to this stuff and my code does not work in IE 6 even though w3schools says it should.

  5. #5
    Join Date
    Jun 2009
    Posts
    9
    I did some research and apparently the answer is locked away inside a book called Bulletproof Ajax. Apparently it outlines and tells you a solid way to get Ajax to pull into just about anything (waiting for my copy to find out if that's true or not). Seems though that loading XML into browsers isn't covered extremely well on the net though.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles