Feeling a little lost in the call flow of Ajax - can anyone provide some guidance?
This is my HTML:
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="myFunction()">Change Content</button>
My JavaScript:
var xmlhttp;
function loadXMLDoc(url, cfunc) {
alert("4");
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
alert("5");
xmlhttp.onreadystatechange = cfunc;
alert("6");
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function myFunction() {
alert("1");
loadXMLDoc("ajax_info.txt", function() {
alert("2");
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert("3");
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
});
}
From what I understand, the Alert box sequence should be
1 2 3 4 5 6
However, it actually appears as
1456222223
Could someone please clarify why the function is being called first? I thought the function couldn't be executed until the parameter values were ready.