Return to Snippet

Revision: 2571
at March 12, 2007 05:49 by 1man


Updated Code
/*Usage
 * var request = getHTTPObject();
 * if(request){
 * AJAX CODE HERE
 * }
 * 
 * If getHTTPObject returns false, the browser isn't Ajax compatible. The if 
 * statement checks to see if it exists, then runs the code.
 */
function getHTTPObject() {
	var xhr = false;//set to false, so if it fails, do nothing
	if(window.XMLHttpRequest) {//detect to see if browser allows this method
		var xhr = new XMLHttpRequest();//set var the new request
	} else if(window.ActiveXObject) {//detect to see if browser allows this method
		try {
			var xhr = new ActiveXObject("Msxml2.XMLHTTP");//try this method first
		} catch(e) {//if it fails move onto the next
			try {
				var xhr = new ActiveXObject("Microsoft.XMLHTTP");//try this method next
			} catch(e) {//if that also fails return false.
				xhr = false;
			}
		}
	}
	return xhr;//return the value of xhr
}

Revision: 2570
at March 12, 2007 05:38 by 1man


Updated Code
/*Usage
 * var request = getHTTPObject();
 * if(request){
 * AJAX CODE HERE
 * }
 * 
 * If getHTTPObject returns false, the browser isn't Ajax compatible. The if 
 * statement checks to see if it exists, then runs the code.
 */
function getHTTPObject() {
	var xhr = false;//set to false, so if it fails, do nothing
	if(window.XMLHttpRequest) {//detect to see if browser allows this method
		var xhr = new XMLHttpRequest();//set var to the new request
	} else if(window.ActiveXObject) {//detect to see if browser allows this method
		var xhr = new ActiveXObject("Microsoft.XMLHTTP");//set var to the new request
	}
	return xhr;//return the value of xhr
}

Revision: 2569
at March 12, 2007 05:37 by 1man


Initial Code
/*Usage
 * var request = getHTTPObject();
 * if(request){
 * AJAX CODE HERE
 * }
 * 
 * If getHTTPObject returns false, the browser isn't Ajax compatible. The if 
 * statement checks to see if it exists, then runs the code.
 */
function getHTTPObject() {
	var xhr = false;//set to false, so if it fails, do nothing
	if(window.XMLHttpRequest) {//detect to see if browser allows this method
		var xhr = new XMLHttpRequest();//set var the new request
	} else if(window.ActiveXObject) {//detect to see if browser allows this method
		var xhr = new ActiveXObject("Microsoft.XMLHTTP");//set var the new request
	}
	return xhr;//return the value of xhr
}

Initial URL

                                

Initial Description
This functions allows you to create a new XMLHTTPRequest, it checks to see if the browser supports each method, if not it returns false.

Initial Title
Ajax getHTTPObject function

Initial Tags
ajax, object, browser

Initial Language
JavaScript