JavaScript
  Home arrow JavaScript arrow Page 3 - Working with AJAX and the Prototype JavaSc...
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  
Mobile Linux 
App Generation ROI 
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

Working with AJAX and the Prototype JavaScript Library
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-02-06

    Table of Contents:
  • Working with AJAX and the Prototype JavaScript Library
  • Using the Prototype/AJAX combination
  • Dealing with formatted (X)HTML results
  • Using the Try.these() function and the each loop

  • 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


    Working with AJAX and the Prototype JavaScript Library - Dealing with formatted (X)HTML results


    (Page 3 of 4 )

    As I stated earlier, the Prototype library will let you deal easily with server results that are formatted as (X)HTML. This process can be quickly accomplished by using the handy "AJAX.Updater" class, which can be used in the following way:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />
    <title>Example Ajax.Updater class</title>
    <script language="javascript" src="prototype-1.4.0.js"></script>
    <script language="javascript">
    // example using the Ajax.Updater class
    function fetchHTML(){
       var ajaxobj= new Ajax.Updater
    ('datacontainer','gethtmldata.php',{method: 'get', parameters:
    'a=1&b=2',onFailure: displayError});
    }
    function displayError(requestObj){
       $('datacontainer').innerHTML='Failed to receive server
    response!';
    }
    window.onload=function(){
       if(document.getElementById && document.createElement &&
    document.getElementsByTagName){
         var btn=$('fetch');
         if(!btn){return};
         btn.onclick=fetchHTML;
       }
    }
    </script>
    </head>
    <body>
      <form>
        <input type="button" value="Fetch HTML" id="fetch" />
     
    </form>
     
    <div id="datacontainer"></div>
    </body>
    </html>

    In this case, the "AJAX.Updater" object accepts, as the first parameter, the ID attribute that corresponds to the web page element where server results will be displayed as (X)HTML. Logically, if your web application expects a response served in this format, this object can be a real time saver.

    Also, it's worthwhile to mention here that the "AJAX.Updater" object accepts the same arguments that were explained concerning the implementation of the "AJAX.Request" class, including the request method to be used, the set of variables sent to the server, and the respective callback functions that will be invoked during the different states of the request in question.

    Okay, at this stage I have shown you how to take advantage of the functionality provided by AJAX when working with the Prototype library. Of course, this is only a simple introduction to the subject. If you want to dig deeper on this topic, please spend some time reading the official documentation.

    Having covered basically the Prototype's AJAX module, in the following section I'll teach you how to use some additional features that are bundled with this library, including the new "Try.these()" function and the Ruby-styled "each" loop.

    So, keep reading to learn more about these interesting topics.

    More JavaScript Articles
    More By Alejandro Gervasio


       · In this second part of the series, you'll learn how to use the excellent AJAX...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - 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






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