XMLHttpRequest Object - 2
Allocate memory for XMLHttpRequest.

<html>
<head>
<title>Check Browser Support XMLHttpObject</title>
<script language="javascript">

var XMLHttpRequestObject=false;

function check()
{

            if(window.XMLHttpRequest)
            {
            XMLHttpRequestObject=new XMLHttpRequest();
            }
            else if(window.ActiveXObject)
            {
            XMLHttpRequestObject=new ActiveXObject("Microsoft.XMLHTTP");
            }

            if(XMLHttpRequest)
            {
            alert ("Your browser support XML HTTP Request Object");
            return;
            }
            else
            {
            alert("Your browser does support XML HTTP Request Object");
            }
          
          
}
</script>
</head>

<body>
<input name="num" type="submit" value="Submit" onClick="check();" />
</body>
</html>

open method of XMLHttpRequest Object

To work with an XMLHttpRequest Object this is necessary to open object.
by user open method and then we connect to server.

Different way to connect with server by open method:-

By get method
XMLHttpRequestObject.open("GET", "data1.php", true);

By post method
XMLHttpRequestObject.open('POST', 'value.php', true);

How to download data from then server

For this purpose we use onreadystatechange properties

ajax does work asynchronously, which means it does not wait server for data, it just attend another tasks.
For this purpose we use onreadystatechange properties. we assigned function to the onreadystatechange properties
each time when the function is called the download status of your data has changed and we check readyState and status properties. in readyState property value  contain 0,1,2,3,4.

1 uninitialized
2 loading
3 loaded
4 interactive
5 complete

Way-1 to use of onreadystatechange properties

XMLHttpRequestObject.onreadystatechange = function()
{
if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status==200)
{
document.getElementById('txt').innerHTML = XMLHttpRequestObject.responseText;
}
}

Way-2 to use of onreadystatechange properties

XMLHttpRequestObject.onreadystatechange = callfunction;

function callfunction()
{
if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status==200)
{
document.getElementById('txt').innerHTML = XMLHttpRequestObject.responseText;
}
}


Previous
Next