How to open an XML file in Firefox

admin

Administrator
Staff member
I pulled the example below from w3schools <!-- m --><a class="postlink" href="http://www.w3schools.com/xml/tryit.asp?filename=try_xmlhttprequest_js">http://www.w3schools.com/xml/tryit.asp? ... request_js</a><!-- m -->
and made a few cosmetic changes - mainly the alerts to track the errors.

It works absolutely fine in IE6 - the path used is ActiveXObject.
In Firefox1.5, the path is XMLHttpRequest, but it fails.

I assume that xml sites themselves do not care whether IE6 or Firefox is
used, so what is wrong?

The alert ('document.onreadystatechange not found') displays, so
onreadystatechange must be non Firefox.
After this alert, nothing happens.
The alerts ('xmlhttp.open ended') and ('xmlhttp.send ended') do not display.

What other code can be used to open the xml file?

BTW, I find it strange that this code is on w3schools with the comment
'//code for Mozilla, etc.' when it doesn't work in Firefox.

<html>
<head>
<script type="text/javascript">
var xmlhttp
//-------------------------------
function loadXMLDoc(url)
{
// code for Mozilla, etc.
if (window.XMLHttpRequest)
// NOTE: THIS PATH DOES NOT WORK IN FIREFOX
{ alert('XMLHttpRequest')
xmlhttp = new XMLHttpRequest()
if (document.onreadystatechange)
xmlhttp.onreadystatechange = state_Change
else
alert ('document.onreadystatechange not found')
// NOTE: THIS ALERT DISPLAYS IN FIREFOX
xmlhttp.open("GET",url,true)
alert ('xmlhttp.open ended')
// NOTE: THIS ALERT DOES NOT DISPLAY IN FIREFOX
xmlhttp.send(null)
alert ('xmlhttp.send ended')
// NOTE: THIS ALERT DOES NOT DISPLAY IN FIREFOX
}

// code for IE
else if (window.ActiveXObject)
{ alert('ActiveXObject')
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
if (xmlhttp)
{ xmlhttp.onreadystatechange = state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send() }
}
}
//-------------------------------
function state_Change()
{
if (xmlhttp.readyState == 4) // "loaded"
{
if (xmlhttp.status == 200) // "OK"
{ alert("XML data OK")
document.getElementById('A1').innerHTML = xmlhttp.status
document.getElementById('A2').innerHTML = xmlhttp.statusText
document.getElementById('A3').innerHTML = xmlhttp.responseText }
else
alert("Problem retrieving XML data:" + xmlhttp.statusText)
}
}
</script>
</head>

<body
onload="loadXMLDoc('http://abc.net.au/news/syndicate/topstoriesrss.xml')">
<h2>Using the HttpRequest Object</h2>

<p><b>Status:</b>
<span id="A1"></span>
</p>

<p><b>Status text:</b>
<span id="A2"></span>
</p>

<p><b>Response:</b>
<br><span id="A3"></span>
</p>

</body>
</html>

--
Cheers,
Trevor L. :confused:
 
Back
Top