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)
            }
        }

No comments: