Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify ActiveXObject JS constructor?

Tags:

I need to wrap an IE ajax request to notify me when it happens. ie i need to know when open is called on this:

var xhr = new ActiveXObject("Microsoft.XMLHTTP");

The only way to do that(i think) is to implement the ActiveXObject constructor to proxy open calls to the real constructor/object. Can you help me do that?

also: i dont need to create the actual xhr object, so please dont tell me to use X framework because its easy.

all i need to know is when open is called (not by my code) on an MS xhr object.

Thank you very much!

like image 716
mkoryak Avatar asked Apr 16 '09 15:04

mkoryak


2 Answers

Since the OP has posted a similar question, and I posted an answer which also happens to fit this question, he asked me to link to my answer, instead of repeating it here.

  • Q: Extending an ActiveXObject in javascript
  • A: A drop-in transparent wrapper for MSXML2.XMLHTTP
like image 62
Tomalak Avatar answered Oct 12 '22 11:10

Tomalak


Perhaps you can try something like this:

XMLHttpRequest.prototype.real_open = XMLHttpRequest.prototype.open;

var your_open_method = function(sMethod, sUrl, bAsync, sUser, sPassword) { 
  alert('an XHR request has been made!');
  this.real_open(sMethod, sUrl, bAsync, sUser, sPassword); 
} 

XMLHttpRequest.prototype.open = your_open_method;

Of course, instead of the alert you can have your own tracking code. I tried it out and it works on 'plain javascript' requests and also requests made with jquery. I think it should work regardless of the framework that was used to make the request.

EDIT April 21 I don't really know how an ActiveXObject can be extended. My guess is that something like this should work:

XHR = new ActiveXObject("Microsoft.XMLHTTP");
var XHR.prototype.old_open = XHR.prototype.open;
var new_open = function(sMethod, sUrl, bAsync, sUser, sPassword) { 
  alert('an IE XHR request has been made!');
  this.old_open(sMethod, sUrl, bAsync, sUser, sPassword); 
} 

XHR.prototype.open = new_open;

Unfortunately (or maybe not) I don't have IE, so I can't test it. But give it a spin and let me know if it did the trick.

like image 28
andi Avatar answered Oct 12 '22 11:10

andi