XML.com: XML From the Inside Out

XML.comWebServices.XML.comO'Reilly Networkoreilly.com
  Articles | Weblogs | Newsletter | Safari Bookshelf
advertisement

Article:
 Very Dynamic Web Interfaces
Subject: A threadsafe implementation for XMLHTTPRequest
Date: 2005-06-06 12:42:15
From: GoatMonkey
Response to: A threadsafe implementation for XMLHTTPRequest

Can you explain how to get data back from this function with the inner function? If I wanted to use this function to send different URLs to it and get back either XML or plain text as a response in a variable.


When I tried it, it seemed to work, but I had to modify the function to just write out the xml to the screen within the inner function. It seems like it should be returned as a variable to whereever it was called from.


Thanks.


No Previous Message Previous Message Move up to Parent Message Up Next Message Next Message


Titles Only Titles Only Newest First
  • A threadsafe implementation for XMLHTTPRequest
    2005-08-05 09:08:18 js9777 [Reply]



    • A threadsafe implementation for XMLHTTPRequest
      2005-09-05 00:37:06 sillyxone [Reply]

      Thanks a lot for the excellent article, so simple but yet powerful. I took a little freedom to modify your codes for my own use (just added a function called handleResponse(), no xml used to simplify the example).


      [ajax.js]
      // threadsafe asynchronous XMLHTTPRequest code
      function handleResponse(response) {
      var sepInd = response.indexOf('|', 0);
      if(sepInd != -1) {
      updateDiv = response.substring(0, sepInd);
      updateCont = response.substring(sepInd + 1);
      //update = response.split('|');
      document.getElementById(updateDiv).innerHTML = 'Done';
      }
      }


      function ajaxSend(url, callback){
      // we use a javascript feature here called "inner functions"
      // using these means the local variables retain their values after the outer function
      // has returned. this is useful for thread safety, so
      // reassigning the onreadystatechange function doesn't stomp over earlier requests.


      function ajaxBindCallback() {
      if (ajaxRequest.readyState == 4) {
      if (ajaxCallback) {
      ajaxCallback(ajaxRequest.responseText);
      }
      else {
      alert('no callback defined');
      }
      }
      }


      // use a local variable to hold our request and callback until the inner function is called...
      var ajaxRequest = null;
      var ajaxCallback = callback;



      // bind our callback then hit the server...
      if (window.XMLHttpRequest) {
      // moz et al
      ajaxRequest = new XMLHttpRequest();
      ajaxRequest.onreadystatechange = ajaxBindCallback;
      ajaxRequest.open("GET", url, true);
      ajaxRequest.send(null);
      }
      else
      if (window.ActiveXObject) {
      // ie
      ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      if (ajaxRequest) {
      ajaxRequest.onreadystatechange = ajaxBindCallback;
      ajaxRequest.open("GET", url, true);
      ajaxRequest.send();
      }
      }
      }




      [test.php]
      <?php
      if (isset($_GET['num'])) {
      echo "uDiv{$_GET['num']}|";
      for ($i = 0; $i < 999999; $i++) // use big data to create a delay in transfer
      echo "$i ";
      exit();
      }
      ?>
      <html>
      <head>
      <script language="javascript" src="ajaxs.js"></script>
      <script language="javascript">
      function updateMe(divNum) {
      ajaxSendG('test.php?num=' + divNum, handleResponse);
      }
      </script>
      </head>
      <body>
      <h2>Testing</h2>
      <?php
      for ($i = 0; $i < 5; $i++)
      print <<< END_BLOCK
      <input type="button" onClick="updateMe({$i})" value="Update Div {$i}">
      <div id="uDiv{$i}"></div>
      END_BLOCK;
      ?>
      </body>
      </html>


      • A threadsafe implementation for XMLHTTPRequest
        2005-09-05 01:12:48 sillyxone [Reply]

        sorry, there is a typo in the above codes, the src of the script should be "ajax.js" instead of "ajaxs.js". Again, thanks a lot for the article and the link to the explaination.

Sponsored By:


Contact Us | Our Mission | Privacy Policy | Advertise With Us | | Submissions Guidelines
Copyright © 2008 O'Reilly Media, Inc. | (707) 827-7000 / (800) 998-9938