Source Code

for file S01_AsyncSamples/CalcFactorsServerSoap.htm

<!DOCTYPE html>
<html lang="en">
<head>
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5" />
  <title>Prime factors calculator</title>
</head>
<body>
  <h1>Prime factors calculator</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 for="outputField">The factors are:</label></th>
        <td><input id="outputField" size="60" disabled="disabled" /></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 using the SOAP encoding
  xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
  xmlObj.Open("POST", "CalcService.asmx", true);
  xmlObj.setRequestHeader("SOAPAction", "http://www.mathertel.de/S01_AsyncSamples/CalcPrimeFactors");
  xmlObj.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
  xmlObj.onreadystatechange = RetrievePrimeFactors;
  var soapText = "<?xml version='1.0' encoding='utf-8'?>"
    + "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"
    + "<soap:Body>"
      + "<CalcPrimeFactors xmlns='http://www.mathertel.de/S01_AsyncSamples/'>"
        + "<inputText>" + inputText + "</inputText>"
      + "</CalcPrimeFactors>"
    + "</soap:Body>"
  + "</soap:Envelope>";
  xmlObj.Send(soapText);
} // CalcPrimeFactors


function RetrievePrimeFactors() {
  var outputText;
  var p;
  
  if ((xmlObj != null) && (xmlObj.readyState == 4)) { //COMPLETED
    // the response is inside the <CalcPrimeFactorsResult> tag
    outputText = xmlObj.ResponseText;
    p = outputText.indexOf("<CalcPrimeFactorsResult>");
    if (p > 0) {
      outputText = outputText.substr(p+24);
      outputText = outputText.substr(0, outputText.indexOf("<"));
    }
    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>This page is part of the <a href="http://ajaxaspects.blogspot.com/">http://ajaxaspects.blogspot.com/</a>
    project.</p>
</body>
</html>


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

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