title: XmlHttpRequest.onload not called
body: <p>I'm playing around with this <code>XmlHttpRequest</code> thing. In some tutorials and books, it is the <code>onload</code> function the one that is called when the request is done. In my little experiment, this function is never called. Here's my code:  </p>
  
  <pre><code>    window.onload = function() {
          var url = "http://www.google.com";
          var request = new XMLHttpRequest();
  
  
          request.onload = function() {
              var state = this.readyState;
              var responseCode = request.status;
              console.log("request.onload called. readyState: " + state + "; status: " + responseCode);
  
              if (state == this.DONE &amp;&amp; responseCode == 200) {
                  var responseData = this.responseText;
                  alert("Success: " + responseData.length  + " chars received.");
              }
          };
  
          request.error = function(e) {
              console.log("request.error called. Error: " + e);
          };
  
          request.onreadystatechange = function(){
              console.log("request.onreadystatechange called. readyState: " + this.readyState);
          };
  
          request.open("GET", url);
          request.send(null);
      };
  </code></pre>
  
  <p>I'm testing this on the last Firefox release (just updated today). The log line in <code>onload</code> is never printed, and the breakpoint I set in the first line is never hit. However, the <code>onreadystatechange</code> function is called twice, and the http request is actually made. This is what firebug's console shows:</p>
  
  <pre><code>    request.onreadystatechange called. readyState: 1
      GET http://www.google.com/    302 Found    174ms
      request.onreadystatechange called. readyState: 4
      NS_ERROR_FAILURE: Failure
          request.send(null);
  </code></pre>
  
  <p>There's an error in the <code>send</code> line. I've tried changing it to <code>request.send()</code> with identical result.</p>
  
  <p>At first I thought this might be the browser trying to prevent XSS, so I moved my html driver page to a Tomcat instance in my dev machine, but the result is the same.</p>
  
  <p>Is this function guaranteed to be called? As I've said above, it's common to be seen in most tutorials, but on the other hand in the <a href="http://www.w3.org/TR/XMLHttpRequest/" rel="noreferrer">W3C spec page</a>, the hello world snippet uses <code>onreadystatechange</code> instead:</p>
  
  <pre><code>    function processData(data) {
        // taking care of data
      }
  
      function handler() {
        if(this.readyState == this.DONE) {
          if(this.status == 200 &amp;&amp;
             this.responseXML != null &amp;&amp;
             this.responseXML.getElementById('test').textContent) {
            // success!
            processData(this.responseXML.getElementById('test').textContent);
            return;
          }
          // something went wrong
          processData(null);
        }
      }
  
      var client = new XMLHttpRequest();
      client.onreadystatechange = handler;
      client.open("GET", "unicorn.xml");
      client.send();
  </code></pre>
  
  <p>It checks <code>readyState == this.DONE</code>. <code>DONE</code> has the value 4, which is what I can see in my log. So if this were a XSS related issue, and the browser were preventing me to make the connection to a different domain, then why the actual connection is made and the DONE status is received???</p>
  
  <p>PS: Yes, I know there are powerful libraries to do this easily, but I'm still a JavaScript noob so I'd like to understand the low level first.</p>
  
  <hr>
  
  <p>UPDATE:<br>
  I've changed the URL to one inside my domain (localhost), and the error is gone but the <code>onload</code> function is still not being called. Tested in IE8 and does not work. Tested in Chrome and works. How's that?</p>
  
  <hr>
  
  <p>UPDATE 2:<br>
  Tested again in Firefox, and now it works. Probably the old page was still cached so that's why I couldn't notice it immediatly. Still failing in IE8, I'll try to test it in a newer version.</p>