Shaikh Sonny Aman’s Blog

previously www.mailtoaman.com

Ajax : A simple and basic example with callback function

Posted on | August 21, 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

One Response to “Ajax : A simple and basic example with callback function”

  1. lineman338
    April 6th, 2008 @ 8:07 am

    well it’s not the kind of news that is worth discussing. i wonder why are you all here so excited?

Leave a Reply