Tutorials >> View >> 8
Yahoo chat: marklester_aover
Bookmark and Share

Simple Ajax Class

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.

Learn the codes below:

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);
 }
});

Click image to download the Zip file: myajax.zip

# of Comments (0)

Add your comment here...

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...

Name : *
Email Add : *
URL (include http://):
Messages : *
Security Code : *
(*) Must not leave blank.
captcha

Our Main Services

We develop and deliver the following services:

Latest News...

Search My Site

 This Site   Web

Google Ads

Google Ads

Powered By:

Advertise

Place your Custom Advertise here $29 for 1 month. Image dimension 276x180 pixels. Click the Paypal "Buy Now" button below.