Custom Search

Wednesday, May 27, 2009

How to Determine and Construct the XMLHttpRequest Object by checking browser type ?

Into JavaScripr file TestScript.js, we need to write the following function to contruct the XMLHTTPRequest object.

var xmlHttpObj;
xmlHttpObj= CreateXmlHttpRequestObject();

 function CreateXmlHttpRequestObject()
        {
        var xmlHttpObj;
        
            if (window.ActiveXObject)
            {
                try
                {
                    xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e)
                {
                    xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
                }
            }
            else
                xmlHttpObj = new XMLHttpRequest();
        
        return xmlHttpObj;
        }

Then we need to call it from any aspx page TestAJAXOne.aspx as following...

    
    
    
    


into the TestScript.js file we need to add these following function 

function MakeXMLHTTPSynchronousCall()
        {
            var xmlHttpObj;
            xmlHttpObj= CreateXmlHttpRequestObject();
            
            if (xmlHttpObj)
            {
                xmlHttpObj.open("GET","http://" + location.host + "/Test/EffectiveDateHAS.xml", false);
                xmlHttpObj.send(null);
                alert("Request/Response Complete.");
            }
        }
        
function MakeXMLHTTPAsynchronousCall()
        {
        
        var xmlHttpObj;
            xmlHttpObj= CreateXmlHttpRequestObject();
            if (xmlHttpObj)
            {
                xmlHttpObj.open("GET","http://" + location.host + "/Test/EffectiveDateHAS.xml", true);
                
                xmlHttpObj.onreadystatechange= function()
                {
                    if(xmlHttpObj.readyState == 4)
                    {
                        alert("Request/Response Complete.");
                    }
                }
                xmlHttpObj.send(null)
            }
        }

What Is the XMLHttpRequest Object?

The XMLHttpRequest object is the heart of all asynchronous operations related to Ajax. It is the object responsible for providing the asynchronous behavior through which Ajax-style applications can interact.
XMLHTTP is a protocol that is designed to package data as XML and send it via the network to a specific destination, or endpoint. This information is typically processed in some way, and the result is returned to the caller. The XMLHttpRequest object is an object that is implemented by most modern browsers to facilitate this communication protocol.