I'm trying to overload the XMLHttpRequest.* method in JavaScript so a webpage can figure out if an Ajax request took place without using any intrusive callbacks. Now, something like this works relatively fine when using most JS frameworks:
XMLHttpRequest.prototype.getResponseHeader = function() {
alert('O hai, looks like you made an AJAX request.');
}
However, there are two catches:
xmlhttp.open("GET","simple.html",false);
Is there any way JS can mirror XMLHttpRequest.open()
or any way that I can chain something to it. I've tried a million paradigms (factory, cloning, wrapping -- most resulting in infinite recursion) and nothing seems to be working. Maybe it's just impossible. Any ideas?
You can keep a reference to the original in some variable before you override the method and then call that variable within the function you overrode:
var temp = XMLHttpRequest.getResponseHeader;
XMLHttpRequest.getResponseHeader = function() { temp.apply(this, arguments); };
That should let you track uses without overriding the functionality provided by the original function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With