The JavaScript routines

 

Now let's look at the Javascript we need to add to our page. There is more explanation of these functions on the Tutorials page.

 

First, a function to generate the XMLHTTPRequest object:

function getXMLHTTPObject() {
try {
req = new XMLHttpRequest();
} catch(err1) {
  try {
  req = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (err2) {
    try {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (err3) {
      req = false;
    }
  }
}
return req;
}

We then need to create the object:

var http = getXMLHTTPObject();

The above code (described elsewhere on the site) allows us to instantiate our XMLHTTPRequest object http.

 

To send a request, we need a function to be called from the button on our page. We use the random number technique described here to avoid problems with the browser cache:

var url = "telltime.php?rand=";
function gettime() {
myRand=parseInt(Math.random()*999999);
http.open("GET", url + myRand, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}

And finally, our callback function handleHTTPResponse() to deal with the data returned by the server:

function handleHttpResponse() {
  if (http.readyState == 4) {
    document.getElementById('showtime').value = http.responseText;
  }
}

<<< Previous   Next >>>

 



This site is brand new and so still somewhat under construction.

Why not bookmark it and call back regularly?

Related:

Links: