AJAX
  Home arrow AJAX arrow Page 2 - Using Google`s Ajax Libraries API
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
Google.com  
AJAX

Using Google`s Ajax Libraries API
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2009-01-05


    Table of Contents:
  • Using Google`s Ajax Libraries API
  • Building a simple Ajax application using a local copy of the Prototype JavaScript library
  • Using Google’s JavaScript Libraries API
  • Versioning with the Google API

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Using Google`s Ajax Libraries API - Building a simple Ajax application using a local copy of the Prototype JavaScript library
    ( Page 2 of 4 )

    Before I show you how to take advantage of Google’s API to work with a specific JavaScript library, I will build a simple Ajax program with the Prototype framework. I'm going to use a conventional approach, that is, I will build the program with a local copy of the framework.

    Once you’ve grasped how this Ajax application works, I will demonstrate how to rebuild it by using Google’s API. In this way you’ll be able to spot the differences in both approaches.

    Having clarified that point, it’s time to begin developing this sample Ajax program. In simple terms, the program will be tasked with reading the contents of a text file, naturally stored in the web server, which will be displayed on screen via the Ajax module that comes bundled with Prototype. Quite easy to grasp, right?

    So, here’s the definition of a basic the text file, called “data.txt,” whose contents will be fetched with Ajax:

    These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file. These are contents of sample file.

    Indeed, the above text file is extremely simple to read, meaning that it’s time to start building the Ajax application that will be charged with retrieving its contents. Below I included the complete source code of this application. Here it is:

    (definition for 'ajax_file_reader.htm' file)


    <!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>Reading file contents with Prototype library</title>

    <style type="text/css">

    body{

    padding: 0;

    margin: 0;

    background: #fff;

    }

    h1{

    font: bold 18pt Arial, Helvetica, sans-serif;

    color: #000;

    }

    #filecontents{

    width: 600px;

    padding: 10px;

    border: 1px solid #999;

    font: normal 10pt Arial, Helvetica, sans-serif;

    color: #000;

    }

    </style>

    <script language="javascript" src="prototype-1.4.0.js"></script>

    <script language="javascript">

    // read file contents with Ajax

    function readFileContents(){

    // send http request

    var ajaxobj=new Ajax.Request('read_file.php',{method: 'get',onComplete: displayFileContents,onFailure: displayError});

    }

    // display file contents

    function displayFileContents(requestObj){

    $('filecontents').innerHTML=requestObj.responseText;

    }

    // display error message

    function displayError(requestObj){

    $('filecontents').innerHTML='Error reading file contents!';

    }

    // initialize file reading application

    function initializeApplication(){

    // attach handler to HTML button

    Event.observe('btn','click',readFileContents);

    }

    // attach handler to window object

    Event.observe(window,'load',initializeApplication,false);

    </script>

    </head>

    <body>

    <h1>Reading file contents with Prototype library</h1>

    <p><input type="button" id="btn" value="Read File Now!" /></p>

    <div id="filecontents"></div>

    </body>

    </html>



    (definition for 'read_file.php' file)


    <?php

    if(!$contents=file_get_contents('data.txt')){

    trigger_error('Error reading file contents',E_USER_ERROR);

    }

    echo $contents;

    ?>


    Even if you’re not familiar with working with the Prototype JavaScript library, it’s pretty easy to see how the above Ajax application functions. It downloads a local copy of the library, and then fetches the content of the previous text file; finally, the application displays it on the browser via an Ajax-based HTTP request.

    In addition, the script that actually reads the target file and sends the data back to the client is the one called “read_file.php,” whose signature is shown above as well.

    To complement the previous explanation, here’s a screen shot that depicts quite clearly how this Ajax-driven programs works:



    Well, at this point I've explained how to build a basic Ajax application by using a local copy of the Prototype framework. Next, I will reconstruct the application by means of the Google API.

    To learn the full details of how this will be accomplished, please read the following section.



     
     
    >>> More AJAX Articles          >>> More By Alejandro Gervasio
     

       

    AJAX ARTICLES

    - PHP AJAX Form Validation
    - Completing a User-Defined CSS Website with P...
    - Create a User-Defined CSS Website with PHP
    - Build A Better Twitter Chat Client Than Cham...
    - Using Division Equations to Make Web Forms S...
    - Using Integer Multiplication to Protect Web ...
    - Using Simple Checksums for Web Form Verifica...
    - Protecting Web Forms with AJAX
    - Using Prototip with AJAX
    - Using Prototip
    - Using the google.load() Method with Google`s...
    - How to Handle Ajax Errors
    - Uncompressing Source Files with Google`s AJA...
    - Using the jQuery Framework with Google`s Aja...
    - Using Google`s Ajax Libraries API




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek