AJAX and newLISP


AJAX like communications with a newLISP scripted site are easy to implement. The following demo shows a date/time ticker constantly updated on a webpage.

Put the following ajax.cgi script and the ajax-demo.html page on a webserver:

#!/usr/bin/newlisp
#
# ajax.cgi - ajax server script

(print "Content-type: text/html\r\n\r\n")

(println (date))
(exit)

# end of file

The client calls the backend newLISP CGI script asynchronously. The following HTML file is the page loaded by the client. In this demo the asynchronous request for a time update is initiated by a JavaScript timer. Instead the update could also be initiated by a client side mousemovement or keypress, detected by JavaScript.

Only a portion of the web page is updated using JavaScript DOM techniques: the div segment named "ajax-demo".

<html>
<!-- ajax-demo.html -->
<body>
<h3>newLISP and AJAX demo</h3>

<div id="ajax-demo">&nbsp;</div>

<p>the time is asynchronously updated by a 
server side newLISP script ajax.cgi.</p>

<p>Do <i>View Source</i> on this page with 
your Browser to see the JavaScript part.</p>

<script type="text/javascript" language="javascript">
if (window.XMLHttpRequest) { 
    xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) { 
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

function callServer(url) {
  xmlHttp.open("GET", url, true);
  xmlHttp.onreadystatechange = updatePage;
  xmlHttp.send(null);
}

function updatePage() {
  if (xmlHttp.readyState == 4) {
    var response = xmlHttp.responseText;
    document.getElementById("ajax-demo").innerHTML = response;
    setTimeout("callServer('ajax.cgi')",1000);
  }
}

callServer("ajax.cgi");
</script>
</body>
</html>
<!-- end of file -->