Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kayza-media/4dcd259f20d908f981f7d09ea5b774c1 to your computer and use it in GitHub Desktop.
Save kayza-media/4dcd259f20d908f981f7d09ea5b774c1 to your computer and use it in GitHub Desktop.
Detect any XMLHttpRequests
// From: http://stackoverflow.com/questions/3596583/javascript-detect-an-ajax-event
var s_ajaxListener = new Object();
s_ajaxListener.tempOpen = XMLHttpRequest.prototype.open;
s_ajaxListener.tempSend = XMLHttpRequest.prototype.send;
s_ajaxListener.callback = function () {
// this.method :the ajax method used
// this.url :the url of the requested script (including query string, if any) (urlencoded)
// this.data :the data sent, if any ex: foo=bar&a=b (urlencoded)
console.log(this.method);
console.log(this.url);
console.log(this.data);
}
XMLHttpRequest.prototype.open = function(a,b) {
if (!a) var a='';
if (!b) var b='';
s_ajaxListener.tempOpen.apply(this, arguments);
s_ajaxListener.method = a;
s_ajaxListener.url = b;
if (a.toLowerCase() == 'get') {
s_ajaxListener.data = b.split('?');
s_ajaxListener.data = s_ajaxListener.data[1];
}
}
XMLHttpRequest.prototype.send = function(a,b) {
if (!a) var a='';
if (!b) var b='';
s_ajaxListener.tempSend.apply(this, arguments);
if(s_ajaxListener.method.toLowerCase() == 'post')s_ajaxListener.data = a;
s_ajaxListener.callback();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment