Hi friends,
Here I present an updated way of using AJAX. In my previous post, I explained the use of AJAX through 3 functions. First one will create object, second will send request, and third will receive the response.
Now I have combined these three functions to one. Here it is...
function getPage(CallPage, DivToPopulate)
{
// Declare Object
var httpObject=false;
if(window.XMLHttpRequest) {
// Opera 8.0+, Firefox, Safari
httpObject=new XMLHttpRequest();
}
else if(window.ActiveXObject) {
//Internet Explorer
httpObject=new ActiveXObject("Microsoft.XMLHTTP");
if(!httpObject){
httpObject=new ActiveXObject("Msxml2.XMLHTTP");
}
}
else{
alert("Your Browser is not compatible to use this application");
}
// Show some default message in the Div, that will contain the Content
document.getElementById(DivToPopulate).innerHTML = 'Loding Content...';//Cache work around
var datevar = new Date();
var timevar = datevar.getTime();
var timestamp= 'TEMP=' + timevar + Math.random();
if (CallPage.indexOf("?") > 0)
{
CallPage = CallPage + '&' + timestamp;
}
else
{
CallPage = CallPage + '?' + timestamp;
}httpObject.open("GET", CallPage, true);
httpObject.onreadystatechange = function(){
if (httpObject.readyState == 4) {
var txt = httpObject.responseText;
// Show content
document.getElementById(DivToPopulate).innerHTML = txt;
}
};
httpObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
httpObject.send(null);
// Function by Vikramjit R.Rai http://www.vikramjits.com
// For any help, feel free to contact vikramjit.rooprai@gmail.com
}
USAGE:
Simply call the getPage function with two arguments. First is the page to be called and second is the DIV to be populated with the contents of the Page called.
that's it...
Happy Coding :)

0 comments:
Post a Comment