Previous
Ajax Feedback Form 
A Gentle Introduction to Ajax
Ajax Library Explained

Ajax Code Explained

In the last page we used a function called 'XMLHTTPObject()' to do the ajax parts of the script. This page attempts to explain the inner working of this script - line by line.


function XMLHTTPObject() {

We create a function called XMLHTTPObject() - this function will return a XMLHttpRequest object based on the browser the visitor is using.


		var xmlhttp = false;

The xmlhttp variable holds the XMLHTTPRequest object. Its initial value is false - if its value is false at the end of the function, that means that the browser used by the visitor don't support advanced javascript that is necessary for AJAX apps.


	//If XMLHTTPRequest is available
	if (XMLHttpRequest) {
		try {xmlhttp = new XMLHttpRequest();}
		catch(e) {xmlhttp = false;}
	} 

The script will look for the XMLHTTPRequest support - this is supported by some browsers - Firefox, Mozilla, Opera, Safari etc. If present, the script will use this feature for AJAX.


else if(typeof ActiveXObject != 'undefined') {
	//Use IE's ActiveX items to load the file.
		try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");} 
		catch(e) {
			try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
			catch(E) {xmlhttp = false;}
		}
	} else  {
		xmlhttp = false; //Browser don't support Ajax
	}

If XMLHTTPRequest is not available, the script tries to see if ActiveXObject is available on the users browser. We need ActiveX support to run AJAX scripts in Microsoft Internet Explorer. Here, two different versions of MSXML are supported - if either is present, we will have AJAX support.


	return http;
}

Here, the function returns the XMLHTTPRequest object if this feature is supported. If it is not support, false will be returned.

Previous
Ajax Feedback Form 
blog comments powered by Disqus