ajax javascript function to create XMLHttpRequest object
-
function getXMLHttp()
-
{
-
var xmlHttp;
-
// Firefox, Opera 8.0+, Safari
-
try { xmlHttp=new XMLHttpRequest(); }
-
// Internet Explorer
-
catch (e)
-
{
-
try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
-
//Older IE?
-
catch (e)
-
{
-
try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
-
catch (e)
-
{
-
alert("Your browser does not support AJAX!"); return false;
-
}
-
}
-
}
-
return xmlHttp;
-
}
-
Use this function to create a new XMLHttpRequest (or ActiveXObject for IE) for use with ajax applications.
Usage:
-
-
function showAjax()
-
{
-
xmlHttp = getXMLHttp();
-
xmlHttp.onreadystatechange=function()
-
{
-
if(xmlHttp.readyState==4)
-
{
-
//update the text of a span called ‘lblajaxreturn’ with the xmlHttp response
-
document.getElementById("lblajaxreturn").innerHTML = xmlHttp.responseText;
-
}
-
}
-
//the url to code that i want to execute
-
var url = "myAjaxFile.php";
-
xmlHttp.open("GET",url,true);
-
xmlHttp.send(null);
-
}
-
You can then execute this function in your html (i.e. onclick=”showAjax”).
Submit to other sitesSam @ February 18, 2007
