title: Ajax requests using loop
body: <p>Can you help me please to integrate this <a href="https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started#Step_3_.E2.80.93_A_Simple_Example" rel="nofollow">ajax code</a> in a for loop?</p>
  
  <p>I am trying to get the href from elements with class "links", and then use them
  to make httpRequests.
     <br>I have seen with firebug, that the httpRequests are sended, and i think the problem is somwere in the alertContents function.
    I struggled all day, but no result.</p>
  
  <pre><code>&lt;a class="links" href="/1"&gt;Link1&lt;/a&gt;
  &lt;a class="links" href="/2"&gt;Link2&lt;/a&gt;
  &lt;a class="links" href="/3"&gt;Link3&lt;/a&gt;
  
  &lt;script&gt;
  for(var i = 0; i &lt; 2; i++) {
  makeRequest(document.getElementsByClassName("links")[i].href);
  }
  
  function makeRequest(url) {
  if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    httpRequest = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // IE
    try {
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) {
      try {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } 
      catch (e) {}
    }
  }
  
  if (!httpRequest) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
  }
  httpRequest.onreadystatechange = alertContents;
  httpRequest.open('GET', url);
  httpRequest.send();
  }
  
  function alertContents() {
  if (httpRequest.readyState === 4) {
    if (httpRequest.status === 200) {
      alert(httpRequest.responseText);
     // here i get the final value of the variable i
    } else {
      alert('There was a problem with the request.');
    }
  }
  }
  &lt;/script&gt;
  </code></pre>