topical media & game development

talk show tell print

basic-ajax-04-XmlHttpRequest2.js / js



  var xHRObject = false;
  if (window.XMLHttpRequest)
  {
  xHRObject = new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
  xHRObject = new ActiveXObject("Microsoft.XMLHTTP");
  } 
  
  function sendRequest(data)
  {
  var bodyofrequest = getBody(data); 
  xHRObject.open("POST", <display.php>, true);
  xHRObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xHRObject.onreadystatechange = getData;
  xHRObject.send(bodyofrequest); 
  }
  
  function getBody(data)
  {
       var argument = "value=";
       argument += encodeURIComponent(data)  
       return argument;
  }
  
  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.