Ajax : A simple and basic example with callback function
Posted on | August 20, 2007 |
The function takes the request url and a function as parameter. The function as parameter is used as call back function whe ajax status is ready.
Feel free to use the code.
function sendRequest(req,fn)
{
// alert(req);
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject(”Msxml2.XMLHTTP”);
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject(”Microsoft.XMLHTTP”);
}
catch (e)
{
alert(”Your browser does not support AJAX!”);
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
fn(xmlHttp.responseText);
}
}
xmlHttp.open(”GET”,req,true);
xmlHttp.send(null);
}
Use it as:
function OnSearch(msg)
{
alert(msg);
}
sendRequest((url),OnSearch);
Comments
Leave a Reply