Ajax
This tutorial will help you learn about ajax, which basically retrieves data from another page without refreshing the current page.
If you want to learn about the history of ajax, what it stands for, then look here. This will just help you actually create an ajax request.
There will be a code sample at the end, but if you want to learn more than I suggest you follow the step-by-step instructions.
We'll start with creating a function.
Code:
function makeRequest(url, id) {
And define http_request...
Code:
var http_request = false;
And then create a request function for Mozilla, Safari etc.
Code:
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
And then create an ActiveXObject for Internet Explorer
Code:
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
And check if the request is created or give an alert and terminate the script
Code:
if (!http_request) {
alert('Error creating XMLHttpRequest()');
return false;
}
Then checking if the information is recieved from the server yet or else give a "Loading..." message
Code:
http_request.onreadystatechange = function() {
var e = document.getElementById(id)
if (http_request.readyState == 4 && http_request.status == 200) {
e.innerHTML = http_request.responseText
}
else e.innerHTML = "Loading..."
};
Then requesting the URL to the server
Code:
http_request.open('GET', url, true);
http_request.send(null);
GET - For URLs that don't need to send POST data
POST - For URLs that do need to send POST data
You need to send data using http_request.send(mydata) with POST, and leave it as null when sending data with GET
Finally then closing the function
And here's the final script.
Code:
<script type="text/javascript">
function makeRequest(url, id) {
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
// See note below about this line
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Error creating XMLHttpRequest()');
return false;
}
http_request.onreadystatechange = function() {
var e = document.getElementById(id)
if (http_request.readyState == 4 && http_request.status == 200) {
e.innerHTML = http_request.responseText
}
else e.innerHTML = "Loading..."
};
http_request.open('GET', url, true);
http_request.send(null);
}
</script>
There are many uses for Ajax, eg. Google Docs, This "Quick Reply" thing at the bottom etc.
Have fun using ajax!
Note - There might be errors in this document, please PM me if you want to report an error, not a question.
Bookmarks