&security

Ajax Server Time

Getting the Server Time with Ajax


Here's an example of getting the server time as described in the tutorial.

Click on the button to get the current server time and display it on the page.




Server time:
 


Here's the complete code:

<html> <head> <title>Ajax Server Time</title> <script language="JavaScript"> var url = 'telltime.php?rand='; function getXMLHTTPObject() { try { req = new XMLHttpRequest(); } catch (err1) { try { req = new ActiveXObject("Msxml12.XMLHTTP"); } catch (err2) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch (err3) { req = false; } } } return req; } var http = getXMLHTTPObject(); // We create the HTTP Object function handleHttpResponse() { if (http.readyState == 4) { document.getElementById('showtime').innerHTML = http.responseText; } } function gettime() { myRand = parseInt(Math.random()*999999); http.open("GET", url + myRand, true); http.onreadystatechange = handleHttpResponse; http.send(null); } </script> </head> <body> <h2>Getting the Server Time with Ajax</h2><br /> <form> <input type=button value="Get Server Time" onclick="gettime()"><br /><br /> Server time: <div id="showtime" style="font:18px bold arial,helvetica, sans-serif; border:1px solid black; width:100px; height: 30px;text-align:center">&nbsp;</div> </form><br /><br /> </body> </html>

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

Why not bookmark it and call back regularly?

Sponsored Links:

Links: