Tracking Ajax

by Ishank Dubey

JavaScript

var startTracing = function(onnew) {

  window.ajaxRequestCount = 0;
  XMLHttpRequest.prototype.uniqueID = function() {
    if (!this.uniqueIDMemo) {
      this.uniqueIDMemo = Math.floor(Math.random() * 1000);
    }
    return this.uniqueIDMemo;
  }
  XMLHttpRequest.prototype.oldOpen = XMLHttpRequest.prototype.open;
  var newOpen = function(method, url, async, user, password) {

    window.ajaxRequestCount++;
    this.oldOpen(method, url, async, user, password);
  }

  XMLHttpRequest.prototype.open = newOpen;
  XMLHttpRequest.prototype.oldSend = XMLHttpRequest.prototype.send;
  var newSend = function(a) {
    var xhr = this;

    var onload = function() {
      window.ajaxRequestCount--;
      /*alert(ajaxRequestComplete);*/
    };

    var onerror = function() {
      window.ajaxRequestCount--;
    };

    xhr.addEventListener("load", onload, false);
    xhr.addEventListener("error", onerror, false);

    xhr.oldSend(a);
  }

  XMLHttpRequest.prototype.send = newSend;
}

startTracing();