DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

XMLHttpRequest For IE

09.15.2005
| 4558 views |
  • submit to reddit
        XMLHttpRequest for IE
 - support onload event handler
 - you can modify "XMLHttpRequest.prototype" for debugging, tracing and AOP style programming.

(function(){
if(typeof ActiveXObject == "function" && typeof XMLHttpRequest == "undefined"){
	XMLHttpRequest = function(){
		var self = this;
		var props = "readyState,responseText,responseXML,status,statusText".split(",");
		this.readyState  = 0;
		this.__request__ = new ActiveXObject("Microsoft.XMLHTTP");
		this.__request__.onreadystatechange = function(){
			for(var i=0;i<props.length;i++){
				try{
					self[props[i]] = self.__request__[props[i]]
				}catch(e){
				}
			}
			self.onreadystatechange();
			self.readyState == 4 && self.onload();
		}
		this.onreadystatechange = function(){};
	}
	var methods = "open,abort,send,setRequestHeader,getResponseHeader,getAllResponseHeaders".split(",");
	var make_method = function(name){
		XMLHttpRequest.prototype[name] = function(){
			var params = new Array(arguments.length);
			for(var i=0;i<params.length;i++) params[i] = "_"+i;
			return Function(
				params.join(","),
				["return this.__request__.",name,"(",params.join(","),")"].join("")
			).apply(this,arguments);
		}
	};
	for(var i=0;i<methods.length;i++) make_method(methods[i]);
}
})();

you can trace request.open like this.
(function(){
var open_old = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function(){
    alert("open called!");
    return open_old.apply(this,arguments)
}
})()
    

Comments

Snippets Manager replied on Mon, 2012/05/07 - 2:15pm

oh,cool!