Nothing new about AJAX Part 1 - XMLHttpRequest Object | 2008-05-24

Technology Blog » Web Development

The term "AJAX" might only stands for Cool stuff in many's mindset when they didn't really program the web language in AJAX way. So, does it mean in AJAX, you have many new stuff need to learn? The answer if No. Whoever has the solide foundation in JavaScript & DOM (Document Object Model) should be very easy to pick it up in an hour......

 

The Secret of  AJAX is the object XMLHttpRequest which is an API that was 1st introduced in Microsoft Outlook Web Access and was standarlized and highly used in Google web applications.

XMLHttpRequest

Microsoft Internet Explorer uses ActiveXObject
Others uses XMLHttpRequest

To deal with it, we need a varible to determine what browser the client is using before you define the XMLHttpRequest.

/*----------------------------------------------------------------
The Following Code is written by Andy Ng <[email protected]>
Site: http://www.PCinvent.com
Please do not removed the above credits/copyrights
------------------------------------------------------------------*/
<script language="javascript" type="text/javascript">
function useAJAX()
{
var xmlHttp; //init Object Variable for the assignment below
Try//1st try
{
//Fire, Opera & Safari
xmlHttp=new XMLHttpRequest();
}
catch(e)
{
    //IE5.5+
    try {//2nd try
    xmlHttp=new ActiveXObject("Msxm12.XMLHTTP");
    }
    Catch(e)
    {
        try //3rd try
        //IE6.0+
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"};
        }
        catch(e)
        {
        alert("Your Browser is not a modern Browser which doesn't support AJAX!!!")
        return false;
         }
         }//close 3rd try
    }//close 2nd try
}//close 1st try
</script>

Of course, you can use If-else for the code above, but try and catch is better to determine the erros if there is any in this case. Hence, you would find most people use try...Catch... for initializing the XMLHttpRequest Object like the example I gave above.