admin管理员组文章数量:1398840
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!
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!
Share Improve this question edited Apr 25, 2009 at 19:21 mkoryak asked Apr 16, 2009 at 15:54 mkoryakmkoryak 58k64 gold badges203 silver badges262 bronze badges5 Answers
Reset to default 3 +100Since 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
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.
There is nothing "to get at real object" it is the real object!
Sounds like you are looking for something like this guy is doing....
http://www.adobepress./articles/article.asp?p=439600
document.write("<script type=\"text/javascript\" src=\"js/HTTP.js\"></script>");
function Ajax()
{
this.toString = function() { return "Ajax"; }
this.http = new HTTP();
this.makeRequest = function(_method, _url, _callbackMethod)
{
this.request = (window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject("MSXML2.XMLHTTP");
this.request.onreadystatechange = _callbackMethod;
this.request.open(_method, _url, true);
this.request.send(_url);
}
this.checkReadyState = function(_id, _1, _2, _3)
{
switch(this.request.readyState)
{
case 1:
document.getElementById(_id).innerHTML = _1;
break;
case 2:
document.getElementById(_id).innerHTML = _2;
break;
case 3:
document.getElementById(_id).innerHTML = _3;
break;
case 4:
return this.http.status(this.request.status);
}
}
}
This has always worked for me.
var xmlHttp;
function yourajaxfunction(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
alert ("Browser does not support HTTP Request");
return;
}
var url="yourserversidecodefile.php";
url=url+"?q="+str;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function stateChanged() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="plete"){
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject(){
var xmlHttp=null;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (E1) {
//Internet Explorer
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (E2) {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
I remend you to use jQuery for AJAX . It's very small but very flexible and fast framework. Check out docs here.
本文标签: javascriptHow to modify ActiveXObject JS constructorStack Overflow
版权声明:本文标题:javascript - How to modify ActiveXObject JS constructor? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744113665a2591409.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论