topical media & game development

talk show tell print

basic-ajax-04-XmlHttpRequest.js / js



  var xHRObject = false;
  
  if (window.XMLHttpRequest)
  {
      xHRObject = new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
      xHRObject = new ActiveXObject("Microsoft.XMLHTTP");
  }
  
  function sendRequest(data)
  {
      
      xHRObject.open("GET", "display.aspx?id=" + Number(new Date) +"&value=" + data, false);
      xHRObject.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT' );
      xHRObject.onreadystatechange = getData;
      xHRObject.send(null); 
  }
  
  function getData()
  {
      if (xHRObject.readyState == 4 && xHRObject.status == 200)
      {
          var serverText = xHRObject.responseText;
  
          if(serverText.indexOf('|' != -1)) 
              {
              element = serverText.split('|');
              document.getElementById(element[0]).innerHTML = element[1];
          }
      }
  }
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.