Cross Browser XMLHttpRequest - Optimized

// retrieveing XMLHttpRequest object - optimized version
// using closure

var xhr = function() {
	if (window.console) console.log( 'xhr()' )
	if (window.XMLHttpRequest) {
		xhr = function() {
			return new XMLHttpRequest();
		};
	} else {
		xhr = function() {
			return new ActiveXObject('Microsoft.XMLHTTP');
		}
	}
	return xhr();
}

//usage
var req = xhr();
var req2 = xhr();

//the second call to xhr will not evaluate the if (window.XMLHttpRequest) again ;)

0 Comments

Post A Comment