0

The following code works fine in firefox but as with many other things, I cant get it working in Internet explorer (any version).

Can anybody help?

<body>
   <script type="text/javascript">
        if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
        else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
        xmlhttp.open("GET","messages.xml",false);
        xmlhttp.send();
        xmlDoc=xmlhttp.responseXML;

        var x=xmlDoc.getElementsByTagName("entry");
        for (i=0;i<x.length;i++)
            {
                document.write("<b>From:</b> ");
                document.write(x[i].getElementsByTagName("username")[0].childNodes[0].nodeValue);
                document.write("<br />");
                document.write("<b>Date:</b> ");
                document.write(x[i].getElementsByTagName("date")[0].childNodes[0].nodeValue);
                document.write("<br />");
                document.write("<b>Message:</b> ");
                document.write(x[i].getElementsByTagName("message")[0].childNodes[0].nodeValue);
                document.write("<br />");
                document.write("<br />");

            }
    </script>

</body>
1
  • Please post the error that you're getting Commented Jun 29, 2012 at 7:52

1 Answer 1

1

Two potential issues spring to mind:

  1. Perhaps your AJAX communication is failing. In which case I would strongly urge you to use a library like JQuery to handle your communication. Then you don't have to worry about the browser compatibility issues

  2. Secondly, the data coming back from the server is not sent with content-type("application/xml"). Different browsers may behave differently if they cannot reliably detect the content type and know that it's XML that you're expecting.

[Edit]

This is a full working example that I tested in IE7

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
   <title></title>
 </head>
 <body>
    <script type="text/javascript">
         if (window.XMLHttpRequest)
             {// code for IE7+, Firefox, Chrome, Opera, Safari
                 xmlhttp=new XMLHttpRequest();
             }
         else
             {// code for IE6, IE5
                 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
             }
         xmlhttp.open("GET","messages.xml",false);
         xmlhttp.send();
         xmlDoc=xmlhttp.responseXML;

         var x=xmlDoc.getElementsByTagName("entry");
         for (i=0;i<x.length;i++)
             {
                 document.write("<b>From:</b> ");
                 document.write(x[i].getElementsByTagName("username")[0].childNodes[0].nodeValue);
                 document.write("<br />");
                 document.write("<b>Date:</b> ");
                 document.write(x[i].getElementsByTagName("date")[0].childNodes[0].nodeValue);
                 document.write("<br />");
                 document.write("<b>Message:</b> ");
                 document.write(x[i].getElementsByTagName("message")[0].childNodes[0].nodeValue);
                 document.write("<br />");
                 document.write("<br />");

             }
     </script>

     <div>Hi</div>

</body>
</html>

And the test data from messages.xml also located at the root of my server

<?xml version="1.0"?>
<root>
    <entry>
        <username>A</username>
        <date>B</date>
        <message>C</message>
    </entry>
</root>

This produces the following output

From: A
Date: B
Message: C

Hi

IE is a pain to debug because it has very limited debugging tools. I checked with FireFox's FireBug add-on to see that the content-type from my WAMP server was "application/xml".

content-type=application/xml

You should also make sure that your XML document is valid by running it through a validator.

Sign up to request clarification or add additional context in comments.

1 Comment

I feel really stupid, at your mention of your WAMP server. I tried running it on my local server.... You guessed it - all working on IE now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.