title: JavaScript XMLHttprequests
body: <p>I got this example demonstrating AJAX from Stoyan Stefanovs <em>Object Oriented JavaScript</em> at page 275. In this example, he is requesting three different files. I have a couple questions if any of you can help.</p>
  
  <ol>
  <li><p>What is  <code>xhr.send('')</code> doing? Why do we need it? I thought GET in the line before was establishign contact with the server, so why this send?<br>
  <em>(The other questions relate to the closure that I don`t fully understand...)</em></p></li>
  <li><p>what exactly is getting passed as a paramater to <code>function(myxhr)</code>?</p></li>
  <li><p>regarding the anonymous function, which has (xhr) passed as a parameter, can you explain at what point in the program xhr gets passed to the anonymous function? For example, is it after <code>xhr.open</code> has taken place? </p></li>
  <li><p>why is function(myxhr) necessary? If it`s to create closure, why is closure necessary here?</p></li>
  <li><p>is parameter <code>(xhr)</code> of the anonymous function getting passed as parameter <code>myxhr</code> in  <code>function(myxhr)</code> once the anonymous function is called?</p></li>
  <li><p>if 5 is true--that <code>xhr</code> is passed as a parameter to <code>function(myxhr)</code>--why is it necessary to change the parameter name from <code>xhr</code> to <code>myxhr</code>?</p></li>
  </ol>
  
  <p>Sample code:</p>
  
  <pre><code>function request(url, callback){
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = (function(myxhr){
      return function() {
        callback(myxhr);
      }
    })(xhr);
    xhr.open('GET', url, true);
    xhr.send('');
  }
  
  request (
    'http://www.phpied.com/files/jsoop/content.txt',
    function (o){
      document.getElementById('text').innerHTML = o.responseText;
    }
  );
  
  request(
    'http://www.phpied.com/files/jsoop/content.html',
    function(o) {
      document.getElementById('html').innerHTML = o.responseText;
    }
  );
  
  request(
    'http://www.phpied.com/files/jsoop/content.xml',
    function(o){
      document.getElementById('xml').innerHTML =
      o.responseXML.getElementsByTagName('root')[0].firstChild.nodeValue;
    }
  );
  </code></pre>