Source Code

for file S01_AsyncSamples/CalcFactorsServer2.htm

<html lang="en">
<head>
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  <title>Prime factors calculator using a WebService</title>
</head>
<body>
  <h1>Prime factors calculator using a WebService</h1>
  <table>
    <tbody>
      <tr>
        <th><label for="inputField">Please enter a number:</label></th>
        <td><input id="inputField" onkeyup="StartCalcPrimeFactors()"></td>
      </tr>
      <tr>
        <th><label>The factors are:</label></th>
        <td><input id="outputField" disabled="disabled" size="60"></td>
      </tr>
    </tbody>
  </table>
  <h3>Hint:</h3>
  <p>try 12313123123123 or 12313123123123123123 for long running calculations ! </p>

  <script type="text/javascript">
var _num = "";
var _timer = null;

var xmlObj = null;

function StartCalcPrimeFactors() {
  var inputText = document.getElementById("inputField").value;
  if (_num != inputText) {
    if (_timer != null)
      window.clearTimeout(_timer);
    document.getElementById("outputField").value = "wait.";
    _num = inputText;
    _timer = window.setTimeout(CalcPrimeFactors, 300, "javascript");
  } // if
}

// calc prime factors
function CalcPrimeFactors() {
  var inputText;

  inputText = document.getElementById("inputField").value;

  if ((inputText == null) || (inputText.length == 0) || (inputText == "0"))
    return;

  if (xmlObj != null) {
    _timer = window.setTimeout(CalcPrimeFactors, 300, "javascript");
    return;
  } // if

  document.getElementById("outputField").value = "wait...";
  
  // call the server with using a http GET command
  xmlObj = null;
  if (window.XMLHttpRequest){ 
    // If IE7, Mozilla, Safari, etc: Use native object
    xmlObj = new XMLHttpRequest()
  } else if (window.ActiveXObject) {
    // ...otherwise, use the ActiveX control for IE5.x and IE6
    xmlObj = new ActiveXObject("Microsoft.XMLHTTP"); 
  } // if
  xmlObj.open("GET", "CalcFactorsRequest.aspx?number=" + inputText, true);
  xmlObj.onreadystatechange = RetrievePrimeFactors;
  xmlObj.send();
} // CalcPrimeFactors


function RetrievePrimeFactors() {
  var outputText;

  if ((xmlObj != null) && (xmlObj.readyState == 4)) { //COMPLETED
    // The result is the whole body of the response
    outputText = xmlObj.responseText;
    xmlObj = null;
    document.getElementById("outputField").value = outputText;
  } // if
} // RetrievePrimeFactors

  </script>

  <hr />
  <p>This sample uses HTML, Javascript and the XMLHTTP object on the client to call
    a WebService.</p>
  <p>A asychronous call is used and the client doesn't get blocked.</p>
  <hr />
  <p>
    This page is part of the <a href="http://www.mathertel.de/AJAXEngine/">Aspects of AJAX</a> project.</p>
  <p>
    For updates and discussions see <a href="http://ajaxaspects.blogspot.com/">http://ajaxaspects.blogspot.com/</a>.</p>
  <hr />
</body>
</html>


This page is part of the http://www.mathertel.de/ web site.

For updates and discussions see http://ajaxaspects.blogspot.com/.