Want to have a site that will load faster and have clean URl, use this simple Ajax Class that you can insert into your site to make Ajax base or with ajax functionalities.
var MyAjax = ({
ajax: function(){
var ajaxRequest; // The variable that makes Ajax possible!
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// Something went wrong
alert("Your browser dont support Ajax.");
return false;
}
}
}
return ajaxRequest;
},
/*
Example:
MyAjax.loadAjax('divResult', 'function.php?task=show&result=HelloWorld', 'inner');
*/
loadAjax: function(id, query, type){
var ajaxRequest = this.ajax();
ajaxRequest.onreadystatechange = function(){
var state = ajaxRequest.readyState;
var status = ajaxRequest.status;
var response = ajaxRequest.responseText;
if(state == 4){
// The response is complete; you can get the server's response and use it.
//alert(ajaxRequest.getAllResponseHeaders());
if (status == 200) {
if(type=='inner'){
document.getElementById(id).innerHTML = response;
} else {
document.getElementById(id).value = response;
}
} else if(status == 404){
alert("Requested URL is not found.");
} else if(status == 403) {
alert("Access denied.");
} else {
alert("Status is "+status);
}
}
}
ajaxRequest.open("GET", window.location+query, true);
ajaxRequest.send(null);
}
});
Please feel free to make a comment and suggestion on this tutorial. Your comment and suggestion will help improve the tutorial for the benefit of you and others. Thank you...