Well the AJAX is completely wrong ;) , let me explain how AJAX works:
- You create a XMLHTTP request object
- You define a function that is triggered when the readystate is changed of the XMLHTTP object
- You open a url using the XMLHTTP object
- You send some data via the XMLHTTP object
- Everytime the readystate and status change, the function you defined at step 2 is called
Here is an example of an AJAX function (that loads the url):
function loadurl(url) {
var ajax = false;
// Create the object:
// Choose the objecttype, depending on what is supported:
if (window.XMLHttpRequest) {
// IE 7, Mozilla, Safari, Firefox, Opera, most browsers:
ajax = new XMLHttpRequest();
} else if (window.ActiveXObject) { // Old IE-browsers
// Make the type Msxml2.XMLHTTP, if possible:
try {
ajax = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e1) { // Else use the other type:
try {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) { }
}
}
// Retrieving the data from the page
if (ajax) {
alert("url: " + url);
ajax.open('GET', url);
// Sends request
ajax.send(null);
// Function that handles response
ajax.onreadystatechange=function(){
// If everything is OK:
if ( (ajax.readyState == 4) && (ajax.status == 200) ) {
// Returns the value to the document
alert(ajax.responseText);
}
}
} else { // AJAX is not useable
alert('It is not possible to connect, please update your browser.');
}
}
~G