ajax - how Create an XMLHttpRequest Object

the xmlhttprequest object is the key to ajax.


AJAX xmlhttprequest object

the xmlhttprequest(xhr) is an api available in web browser scripting languages such as javascript. it is used to send http or https requests directly to a web server and load the server response data directly back into the script.the data might be received from the server as json ,xml html or as plain text.

all modern browsers support the xmlhttprequest(xhr) object (ie5 and ie6 use activexobject).


how create an xmlhttprequest(xhr) object

internet explorer versions 5 and 6 did not define the xmlhttprequest object its uses an activex object.

the syntax for creating an activexobject :--

var ref = new activexobject("microsoft.xmlhttp");

all modern browsers such as firefox, safari, opera, chrome and ie7+ fully support the xmlhttprequest object.

the syntax for creating an xmlhttprequest object.

var ref = new xmlhttprequest () ;


browser specific codes.

simply way to create your source code compatible to a browser is to use if... else if blocks in your javascript.

    <script>
                  //browser support code  
                  
     function  getajax() {
                                            
                                                
          var  ajaxobject  =  null;
                                            
             if (window.xmlhttprequest) {
                                                    
                                           //support ie7+, firefox, chrome, safari, opera.
                    
                                                                              
                     ajaxobject   =  new    xmlhttprequest () ;
                                                  
                   
                    } else if (window.activexobject)  { // code for  ie5 ,ie6.
                                                   
                           
                           ajaxobject  =  new activexobject("microsoft.xmlhttp");       
                                                           
                 }
                           return  ajaxobject;
                                                          
              }                      
                                                
    </script>

creat code using try{} catch(e){} blocks.

    <script>
                  //browser support code  
                  
     function  getajax() {
                                            
                                                
          var  ajaxobject  =  null;
                                            
             try{    //support ie7+, firefox, chrome, safari, opera.
                    
                                                                              
                     ajaxobject   =  new    xmlhttprequest () ;
                                                  
                   
                    } catch (e)  { // code for  ie5 ,ie6.
                                                   
                try {                   
                           ajaxobject  =  new activexobject("msxml2.xmlhttp");
                           
               
                     } catch (e)  {
                              
                         try{
                                                    
                        ajaxobject = new  activexobject("microsoft.xmlhttp");
                                                  
                              }catch (e){ //somethid was wrong }
                                                    
                                                   
                  }            
                                                           
                 }
                                                          
              }                      
                                                
    </script>