Using XMLHttpRequest in Firefox



I pulled the example below from w3schools and made a few cosmetic changes

It works absolutely fine in IE6 - the path used is ActiveXObject.
In Firefox, 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.
Website: http://tandcl.homemail.com.au


.



Relevant Pages

  • Re: Sudden alert
    ... the time I had done before with no problems) when a critical alert ... came up stating that some components of firefox were being blocked ... but with windows firewall stopping nero showtime from accessing the ... notified if any extentions are trying to install themselves (although ...
    (microsoft.public.windowsxp.general)
  • Re: Sudden alert
    ... the time I had done before with no problems) when a critical alert ... came up stating that some components of firefox were being blocked ... but with windows firewall stopping nero showtime from accessing the ... notified if any extentions are trying to install themselves (although ...
    (microsoft.public.windowsxp.general)
  • Re: Script works correctly in Firefox, not in IE6
    ... that the following code snippet works OK in Firefox but not in IE6: ... alert ("Please select Dog Sex"); ... is recognized and the alert is displayed. ...
    (comp.lang.javascript)
  • Re: Use of variable causes "Object required", but if alert is calle, everything works...
    ... In firefox, the code runs perfectly, and the appropriate styles ... > The funny thing is, when I add an alert call before I set the styles, ... > FireFox bug ... frame's parent), then it runs parallelly. ...
    (microsoft.public.scripting.jscript)
  • Re: Script works correctly in Firefox, not in IE6
    ... the following code snippet works OK in Firefox but not in IE6: ... alert ("Please select Dog Sex"); ... is recognized and the alert is displayed. ...
    (comp.lang.javascript)

Loading