Skip to content

Instantly share code, notes, and snippets.

@dgellow
Last active December 10, 2015 06:59
Show Gist options
  • Save dgellow/4398130 to your computer and use it in GitHub Desktop.
Save dgellow/4398130 to your computer and use it in GitHub Desktop.
Ajax sample
loadAsynchronous = function () {
var target = document.getElementById("divAsynchronous");
target.innerHTML = "Loading ...";
var request = new XMLHttpRequest();
var content = "";
request.onreadystatechange = function () {
if (request.readyState == 1) {
target.innerHTML = "Server connection established";
}
if (request.readyState == 2) {
target.innerHTML = "Request received";
}
if (request.readyState == 3) {
target.innerHTML = "Processing request";
}
if (request.readyState == 4) {
if (request.status == 200) {
target.innerHTML = "";
xmlDoc = request.responseXML;
artists = xmlDoc.getElementsByTagName("ARTIST");
for (i = 0; i < artists.length; i++){
target.innerHTML += artistName[i].childNodes[0].nodeValue + "<br />";
}
} else {
target.innerHTML = "Request finished and response is ready. But my status is " + request.status;
}
}
}
request.open("GET", "catalog.xml?t=" + Math.random(), true);
request.send();
}
loadSynchronous = function () {
var target = document.getElementById("divSynchronous");
var request = new XMLHttpRequest();
content = "";
request.open("GET", "catalog.xml", false);
request.send();
var xmlDoc = request.responseXML;
target.innerHTML = null;
artists = xmlDoc.getElementsByTagName("ARTIST");
for (var i = 0; i < artists.length; i++) {
target.innerHTML += ((content = artists[i].childNodes[0].nodeValue) !== "" ? content + "<br>" : "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment