Ajax request
Simple reusable asynchronous multibrowser ajax function.
Call a page by javascript and pass the html result to a function when it arrives (. The url to load by HTTP GET method, and the callback function name are passed as parameters. All the litteral content of the result page is passed as parameter.
Note: some security features, like in Firefox, can block AJAX request to other domains than the domain of the calling webpage (where the script is executed from).
function ajaxRequest(url, callbackFunction)
{
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined')
{
xmlhttp = new XMLHttpRequest();
}
xmlhttp.open("GET", url,true);
if(callbackFunction != null)
{
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState == 4)
{
callbackFunction(xmlhttp.responseText);
}
}
}
xmlhttp.send(null);
}
function sampleCallBackFunction(httpResponseText)
{
alert(httpResponseText);
}
// sample calls
//
ajaxRequest("http://www.someurl.com",sampleCallBackFunction);
ajaxRequest(document.location,sampleCallBackFunction);//show current page source