Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting data from an XML tag in IE 1

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
Dear All,

I was just browsing through a magazine and noticed an article on AJAX that had a small (and very briefly explained) example. It didn't work first time so I tinkered around with the appropriate code (in the processResponse function) until it worked in Firefox. I browsed the same page in IE and I got a blank page instead of some data in my "result" div tag.

I debugged the code and found that IE was stopping on the line highlight in blue below. I looked up getElementsByTagName and I seem to be using it correctly. But is this one of those cases where IE is not following standards or have I made some error?

AjaxExample.xml
Code:
<?xml version="1.0" encoding="iso-8859-1"?>

<days>
  <day name="Sunday">Mow the lawn.</day>
  <day name="Monday">Pay the rent.</day>
  <day name="Tuesday">Wash the dog.</day>
  <day name="Wednesday">Do the laundry.</day>
  <day name="Thursday">Important Meeting.</day>
  <day name="Friday">Meal out.</day>
  <day name="Saturday">Go to the match.</day>
</days>

AjaxExample.htm
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">

<head>
  <title></title>
  <script language="Javascript" type="text/javascript">
    <!-- Hide script from old browsers
    function createRequestObject()
    {
      var requestObj;
      var browser = navigator.appName;
      if(browser == "Microsoft Internet Explorer")
      {
        requestObj = new ActiveXObject("Microsoft.XMLHTTP");
      }
      else
      {
        requestObj = new XMLHttpRequest();
      }
      return requestObj;
    }

    var http = createRequestObject();

    function makeRequest()
    {
      http.open('get', 'AjaxExample.xml');
      http.onreadystatechange = processResponse;
      http.send(null);
    }

    function processResponse()
    {
      if (http.readyState == 4)
      {
        var xmldoc = http.responseXML;
        var dayIndex = document.activities.select_day.selectedIndex;
        [COLOR=blue]var myActivity = xmldoc.getElementsByTagName("day")[dayIndex].firstChild.data;[/color]
        alert(myActivity);
        document.getElementById('result').innerHTML = myActivity;
      }
    }
    // End hiding script -->
  </script>
</head>

<body>

  <div id="query">
    <p>Select a day from the drop-down menu:</p>
    <form name="activities">
      <select id="select_day" onchange="makeRequest()">
        <option value="Sunday">Sunday</option>
        <option value="Monday">Monday</option>
        <option value="Tuesday">Tuesday</option>
        <option value="Wednesday">Wednesday</option>
        <option value="Thursday">Thursday</option>
        <option value="Friday">Friday</option>
        <option value="Saturday">Saturday</option>
      </select>
    </form>
  </div>

  <div id="result"></div>

</body>

</html>

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I think you are testing the mechanism on a local file system rather than retrieve ("get") the xml from a webserver (localhost or else). If it is on a local file system, you have work out a workaround. In particular you'll see http.status=0 is returned once it is ready (readystate=4). Can you confirm that it is on a local file system?
 
Yes, it is hosted on a local hard drive. I'm just trying to get a feel for how this works - it does not have to necessarily work from a local file system, so I might just upload it to a webserver. What would a status of 0 indicate? Is this value applicable to web & local hosting or just local hosting?

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
[1] For an okay signal on the http:// protocol, it would be =200, whereas on a local file system file:// is just return =0.
[2] On a local, you can work around like this. Put it inside the readystate==4 conditional.
[tt]
if (browser == "Microsoft Internet Explorer") {
if (http.status==0 && ) {
var xmldoc=new ActiveXObject("Msxml2.DOMDocument")
xmldoc.loadXML (ohttp.responseText);
} else { //need be .status==200 to look thing more broadly
var xmldoc=ohttp.responseXML;
}
}
[/tt]
The above is for ie part. (You know the part for mos.) Then keep the rest. You may test it out?
 
Correct a typo: the line
[tt]if (http.status==0 [highlight]&&[/highlight] ) {[/tt]
should of course be read
[tt]if (http.status==0) {[/tt]

 
I relist. (I used ohttp instead of your http which is fine, only my preference.)
[tt]
if (browser == "Microsoft Internet Explorer") {
if (http.status==0 ) {
var xmldoc=new ActiveXObject("Msxml2.DOMDocument")
xmldoc.loadXML (http.responseText);
} else { //need be .status==200 to look thing more broadly
var xmldoc=http.responseXML;
}
}
[/tt]
 
My original code works perfectly when I run it from a web server - thanks for that advice tsuji - I thought it was a code issue.

Your workaround is excellent - thank you very much. I checked http.status and it confirmed what you said (web-hosted returned 200, locally-hosted returned 0). The script now works in IE & FF for both web-hosted and locally-hosted files!!!

Here is the modified processResponse function if anyone is interested.
Code:
function processResponse()
{
  if (http.readyState == 4)
  {
    if (browser == "Microsoft Internet Explorer")
    {
      if (http.status == 0 )
      { // for files stored on a local file system (browsed with IE)
        var xmldoc = new ActiveXObject("Msxml2.DOMDocument")
        xmldoc.loadXML(http.responseText);
      }
      else
      { // for files stored on a web server (browsed with IE)
        var xmldoc = http.responseXML;
      }
    }
    else
    { // for all files (browsed with a non-IE browser)
      var xmldoc = http.responseXML;
    }
    var dayIndex = document.activities.select_day.selectedIndex;
    var myActivity = xmldoc.getElementsByTagName("day")[dayIndex].firstChild.data;
    document.getElementById('result').innerHTML = myActivity;
  }
}

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top