RussellHarrower
RussellHarrower

Reputation: 6830

xmlhttp not working

I'm not sure what I am doing wrong. I placed this in the body, next to the div myDiv and it seems not to run, so I don't know what I am doing wrong.

<script type="text/javascript" language="javascript">
  var xmlhttp;
  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","http://www.example.com/test.php",false);
  xmlhttp.send();
  document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
</script>
<div id="myDiv"></div>

Upvotes: 0

Views: 442

Answers (1)

Paul Fleming
Paul Fleming

Reputation: 24526

The script runs before the div "myDiv" is rendered. Swap them over...

<div id="myDiv"></div>

<script type="text/javascript" language="javascript">
var xmlhttp;
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","http://www.example.com/test.php",false);
xmlhttp.send();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

Upvotes: 2

Related Questions