// this function simply defines the object that you're going to use for the AJAX ...
// no matter how you do it (on load, on click, every 3 seconds), you'll need this
function createRequestObject() {
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}
// you will always need this line too, it simply sets up an object using the above function ...
// you can name it whatever you want, I've used http in all of my scripts just for uniformity so I don't get confused.
var http = createRequestObject();
// this function can be completely messed around with, you can rename it if you want, you can change what's passed to it (parameters)
// the "meat" of the function should always be there (open, onreadystatechange, and the send) but you can change everything else
// you can have it send different parameters too.
function sndReq(action) {
// basically before this part, setup whatever it is you need to send, just form a string to pass to the script the get parameters
http.open('get', 'rpc.php?action='+action);
// the following 2 lines should always be the end of the function
http.onreadystatechange = handleResponse;
http.send(null);
}
// the way AJAX works ... it's not like you have the "sndreq" function above and thats it ... because it threads and fetches
// while the browser is doing other things, it splits off ... you should always have this function, but you'll need to modify it for your purposes
// this function basically "catches" what send request function asked for.
function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
// I took out some of the stuff from the tutorial here ...
// this is now a template for whatever you want, the var "response" has everything in the webpage's html
// so if you made the html simply return a number or whatever, you can then change something on the page to match that number,
// eg: document.getElementById('someitem').innerHTML = response;
}
}
// once you make something using this template, the idea is simple... you call the sndReq() function, however you've made it.
// for what you're talking about, most likely you would have something like this:
function keepUpdatingData() {
sndReq(whatever it is you need to pass to the script); // then configure the sndReq function to get the right url based on this
window.setTimeout('keepUpdatingData()',3000); // do this again in 3 seconds
}
// then you would have in your body tag, onLoad="keepUpdatingData()"