From EduTech Wiki
Jump to: navigation, search

1 Definition

According to Wikipedia: “Ajax, shorthand for Asynchronous JavaScript and XML, is a web development technique for creating interactive web applications. The intent is to make web pages feel more responsive by exchanging small amounts of data with the server behind the scenes, so that the entire web page does not have to be reloaded each time the user requests a change. This is meant to increase the web page's interactivity, speed, and usability.”, retrieved 15:11, 9 February 2007 (MET).

The Ajax technique uses a combination of:

  • XHTML (or HTML) and CSS, for marking up and styling information.
  • The DOM accessed with a client-side scripting language, usually ECMAScript (JavaScript)
  • The XMLHttpRequest object is used to exchange data asynchronously with the web server.
  • XML is sometimes used as the format for transferring data between the server and client.

2 Standards and reference

The XMLHttpRequest object implements an interface exposed by a scripting engine that allows scripts to perform HTTP client functionality, such as submitting form data or loading data from a server. It's the core functionality of so-called AJAX and works in most browsers, but not exactly the same way. Standardization may happen in some near future. DKS/April/2008

  • The XMLHttpRequest Object (W3C Working Draft, April 2008). This specification proposal also outlines dependencies with other specifications like DOM, HTML 5, and HTTP.

3 Interesting AJAX applications

Discussed in this wiki

4 A simple example

AJAX is really simple. The difficult part is to write really good interfaces (see toolkits in the links section) and to do something with these data on the server side ...

  • Below we just demo a simple HTML page that will talk to php (Disclaimer: Daniel K. Schneider is not a programmer).
  • Example files are here
    <title>Simple Ajax example</title>
    <script type="text/javascript" language="javascript">

var url;
var table;

function init () {
  url = "ajax1.php";
 // url = "ajax-debug.php";
  table = document.getElementById("table1");

function makeRequest(element) {
  // This function is called from the HTML code below
  // element is the DOM element (tag on which the user clicked)

  var http_request = false;
  // ---- Mozilla, Safari, etc. browsers
  if (window.XMLHttpRequest) { 
    http_request = new XMLHttpRequest();
    // This will make sure that the server response claims to be XML (in case we retrieve something else)
    if (http_request.overrideMimeType) {
    // ---- IE browsers
  } else if (window.ActiveXObject) { 
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
	http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
  // ---- abort if there is no reply
  if (!http_request) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
  // We register the function that will deal with a reply
  http_request.onreadystatechange = function() { processServerReply(http_request); };
  // This lines starts building the request
  http_request.open('POST', url, true);
  // Contents WE send from here will be urlencoded
  http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  // here we extract the contents of tag on which the user clicked
  user_pref    = element.innerHTML;
  // This is the content of the request
  // alert(user_pref);
  user_request = "user_pref_fruit=" + user_pref;
  // We send the data - data are query strings

function processServerReply(http_request) {

  if (http_request.readyState == 4) {
    if (http_request.status == 200) {
      // We tell the server that we want to deal with XML as a DOM Document !!
      replyXML = http_request.responseXML;
      treatResponse (replyXML);
      // displayResponse (replyXML);
    } else {
      alert('There was a problem with the request.');

// This will change the HTML contents of the page
function treatResponse (reply) {
  // reply is a XML DOM datastructure !!
  // extract some XML - we know that it is in an "answer" tag
  // DOM HTML will not work, it's XML here 
  var answer = reply.getElementsByTagName('answer').item(0).firstChild.nodeValue;

  // new tr, td elements
  var element_tr = document.createElement("tr");
  var element_td = document.createElement("td");
  // contents for the td element
  var text    = document.createTextNode(answer);

// just for debugging, will open up a popup window. Useful if you tell the php  to send debugging infos...
function displayResponse (reply) {



  <body onload="init()">
    <h1>Simple Ajax example</h1>

    <strong>Please</strong> click on a fruit:

      <li>I like
	  style="cursor: pointer; text-decoration: underline"
      <li>I like
	  style="cursor: pointer; text-decoration: underline"

      <li>I like
	  style="cursor: pointer; text-decoration: underline"



 Dialog history:
   <table border id="table1">

	  <!-- one of (TD TH) -->
	  <th>Server replies</th>


header ("Content-type: application/xml");   
echo '<?xml version="1.0" encoding="ISO-8859-1" ?>';

if (array_key_exists('user_pref_fruit', $_POST))  
  { $user_pref  = $_POST['user_pref_fruit']; }

echo "<answer>";
echo "Oh you like " . $user_pref . " !";
// echo "Oh you like " . $user_pref . " !" . " - Query String=" .  $_SERVER["QUERY_STRING"];
echo "</answer>";


5 Software

5.1 JavaScript toolkits

jQuery library
Dojo (free software)
Aculo (free software)
  • “ script.aculo.us provides you with easy-to-use, cross-browser user interface JavaScript libraries to make your web sites and web applications fly.” (retrieved 15:11, 9 February 2007 (MET))
  • Aculo Home Page
  • Script.aculo.us documentation wiki
Prototype.js library

6 Links

7 References

7.1 Technical how-to

  • Vlad Kofman (2007), The Web 2.0 Movement Is Here. But What Does It Mean to You?, Developper.com Atricle, HTML