title: Running a PHP through Ajax on Unload
body: <p>I'm trying to have a php script run when the user navigates away from the page. This is what I'm using currently:</p>
  
  <pre><code>function unload(){
  var ajaxRequest;  // The variable that makes Ajax possible!
  
  try{
      // Opera 8.0+, Firefox, Safari
      ajaxRequest = new XMLHttpRequest();
  } catch (e){
      // Internet Explorer Browsers
      try{
          ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
          try{
              ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e){
              // Something went wrong
              window.location = "unsupported.html";
              return false;
          }
      }
  }
  
  ajaxRequest.open("GET", "ajax/cancelMatch.php", true);
  ajaxRequest.send(null); 
  }
  </code></pre>
  
  <p>Through FireBug, it looks like it is calling the open function of the ajaxRequest Object, but the PHP doesn't run! Is this something to do with the fact that it's calling it on the unload of the page?</p>
  
  <p>Also, I've found an event called onbeforeunload, but I can't figure out how to get it working, if it still is available.</p>