Source code

This is the source code of the simplified library AJAX application.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Very simplified library client application</title>
 
<!-- In the whole application, only two lines must be changed/added from a
     normal AJAX code.  This is the first one:  we include beepbeep.js. -->
<script type="text/javascript" src="beepbeep.js"></script>
 
<script type="text/javascript">
// Creates and sends an XML mesasge based on the list of books in the form
function createAndSend() {
  // This is the second line to be changed:  we instantiate the wrapper
  // class XMLHttpRequestBEEP instead of XMLHttpRequest or MSXML
  var req = new XMLHttpRequestBEEP();
  // We create the message from the form content
  var booksform = document.getElementById("inputbooks");
  var messageContent = "<message>\n";
  if (booksform.action[0].checked)
    messageContent += "<action>borrow</action>\n";
  else
    messageContent += "<action>return</action>\n";
  bookArray = booksform.booklist.value.split(" ");
  messageContent += "<books>\n";
  for (var bookno in bookArray)
    messageContent += "<book>" + bookArray[bookno] + "</book>\n";
  messageContent += "</books>\n";
  messageContent += "</message>";
  alert(unescape(messageContent));
  // Posts the message.  For the sake of the example, we just pretend:  the
  // "true" 2nd param of send prevents the message from actually being sent
  req.open("POST", "myserver.com", true); 
  req.send(messageContent, true);
  // Remark how nothing in the previous code bothers with the fact we
  // are actually monitoring some form of contract
} 
</script>
</head>
<body>
 
<h1>Very simplified library client application</h1>
 
<p>Go back to a <a href="../../tour.php">description</a> of what it does,
see the source code, etc.</p>
 
<form id="inputbooks" action="#">
<fieldset>
<p>Enter a list of book IDs (numbers) separated by spaces.</p>
<input type="text" name="booklist" size="20"/>
<ul>
<li><input type="radio" name="action" value="enter" checked="checked"/>
  I borrow these books</li>
<li><input type="radio" name="action" value="exit"/>
  I return these books</li>
</ul>
<input type="button" value="Send the message" onclick="createAndSend()"/>
</fieldset>
</form>
 
<p>Below is the monitor applet.  Normally it is invisible and runs in the
background; we display it for the sake of the example.  The display
<i>a/b/c/:d:e</i> indicates:  a/b/c:  state of properties 1/2/3
(? = unknown, T = true, F = false); d: number of messages processed,
e: total processing time (in ms).</p>
 
<!-- The applet does not need to be added; loading beepbeep.js adds it to
     the HTML at runtime. -->
</body>
</html>
 

Main menu

SourceForge.net Logo