<!DOCTYPE html>
<html>
<body>
<p id="demo">Let AJAX change this text.</p>
<button type="button" onclick="CallAJAX()">Change Content</button>
<script>
function data_in()
{
alert("data came in.");
}
// The function will wait for the information and only then will it continue.
// Caution in such use as JavaScript may get stuck until the information arrives.
function CallAJAX(filename,divide, func)
{
var xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById(divid).innerHTML = this.responseText;
func()
}
xhttp.open("GET", filename, false); // false insted to true
xhttp.send();
document.getElementById("demo").innerHTML = xhttp.responseText;
}
CallAJAX("new_text.txt","demo", data_in);
</script>
</body>
</html>