We help IT Professionals succeed at work.

I am confuse about the sequence of

goodk
goodk asked
on
Does the following lines get's executed in the begining or the end;  


xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();

http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
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.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
Comment
Watch Question

Commented:
xmlhttp.send() will execute GET operation to ajax_info.txt defined in xmlhttp.open()

is this what you ask?

Author

Commented:
when does this gets called;  
xmlhttp.onreadystatechange=function()

what is sequence of the execution of the example.

thanks
Commented:
Once send() command is executed asynchronously, the onreadystatechange will be triggered when xmlhttp request end with receiving some data or getting an error.

You can check if the data is returned by checking readyState == 4 and status == 200.
If both conditions are true, you can access responseText for the returned string.

Author

Commented:
got that,

What about the following part?  The sequence still seems confusing.  What is throwing me off is the function inside the function?

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");
  }

Author

Commented:
thanks

Commented:
Ok, the part you mention above is just assignment part. It is to assign XMLHTTP object to xmlhttp variable by testing if window.XMLHttpRequest is available or not which is available in IE7+, Firefox, Chrome, Opera and Safari while the else part is for IE5 and 6.

The xmlhttp.onreadystatechange=function()... is also an assignment to bind a given function to an event.

Author

Commented:
if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById(VarmyDiv).innerHTML=xmlhttp.responseText;
    }


What if myDiv was a variable, VarmyDiv?  How do we pass the value to it when we call the

 loadXMLDoc(string VarmyDiv)?? thanks