JavaScript
  Home arrow JavaScript arrow Page 3 - Learning AJAX
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Learning AJAX
By: Mamun Zaman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2007-08-20

    Table of Contents:
  • Learning AJAX
  • The XMLHttpRequest Object
  • The Complete File
  • A Simple Example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Learning AJAX - The Complete File


    (Page 3 of 4 )

    Now you can use this response in your current web page. We now have AJAX ready to use. You can add these JavaScript codes to one file and name it ajax.js. This is a better practice. Here is the complete ajax.js.

    function createRequestObject(){

      var req;

      try
     
    {
        // Firefox, Opera, Safari
        req = new XMLHttpRequest();
      }

      catch (e)
     
    {
        // Internet Explorer
        try
       
    {
         
    //For IE 6
          req = new ActiveXObject("Msxml2.XMLHTTP");
        }

        catch (e)
       
    {
          try
         
    {
            //For IE 5
           
    req = new ActiveXObject("Microsoft.XMLHTTP");
          }

          catch (e)
         
    {
            alert('Your browser is not IE 5 or higher, or Firefox or Safari or Opera');
         
    }
        }
      }

      return req;
    }

    //Make the XMLHttpRequest Object
    var http = createRequestObject();

    function sendRequest(method, url){
     
    if(method == 'get' || method == 'GET'){
       
    http.open(method,url,true);
       
    http.onreadystatechange = handleResponse;
       
    http.send(null);
     
    }
    }

    function handleResponse(){
     
    if(http.readyState == 4 && http.status == 200){
       
    var response = http.responseText;
       
    if(response){
         
    document.getElementById("ajax_res").innerHTML = response;
       
    }
     
    }
    }

    Here you can see that I put the response as the innerHTML of a DIV with an id equal to "ajax_res". I also created a function called sendRequest which takes two strings as parameters. The first parameter is the HTTP method, either GET or POST. The second parameter is the URL that will be invoked using the XMLHttpRequest object in background. This function will be called from PHP or JSP pages in the next example codes.

    Now add this JavaScript file in your web page like this -

    <script type="text/javascript" language="Javascript" src="ajax.js"></script>

    Then add a JavaScript code to call the sendRequest function of ajax.js. You need a DIV tag to display the response.

    <div id="ajax_res"></div>

    More JavaScript Articles
    More By Mamun Zaman


     

    JAVASCRIPT ARTICLES

    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT