JavaScript
  Home arrow JavaScript arrow Page 3 - Introducing the Prototype JavaScript Libra...
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

Introducing the Prototype JavaScript Library
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-01-30

    Table of Contents:
  • Introducing the Prototype JavaScript Library
  • Handling specific elements of a web page: introducing the $ function
  • Working easily with arrays and hashes: using the $A and $H functions
  • Dealing easily with form fields: using the $F function

  • 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


    Introducing the Prototype JavaScript Library - Working easily with arrays and hashes: using the $A and $H functions


    (Page 3 of 4 )

    Now that I've explained how the $ function works, in this section I'm going to introduce two additional functions included with the Prototype library. These are the $A and $H functions.

    As you'll see shortly, the $A function comes in handy for converting any inputted argument (keep in mind that this function takes only one parameter) into an array object. The advantage of working with array objects will become clear when I cover some of their most relevant methods later on, but for the moment, take a look at the following example. It demonstrates a simple implementation of the $A function.

    The code sample is as follows: 

    <!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 of $A function</title>
    <script language="javascript" src="prototype-1.4.0.js"></script>
    <script language="javascript">
    // example using the $A function
    function displayHTML(){
       var divs=$A($('generalcontainer').getElementsByTagName
    ('div'));
       for(var i=0;i<divs.length;i++){
         var htmlcont=$('htmlcontainer');
         htmlcont.innerHTML+=divs[i].innerHTML;
       }
    }
    window.onload=function(){
       if(document.getElementById && document.createElement &&
    document.getElementsByTagName){
         var btn=$('testbutton');
         if(!btn){return};
         btn.onclick=displayHTML;
       }
    }
    </script>
    </head>
    <body>
    <div id="generalcontainer">
     
    <div>
       
    <p>This is a sample paragraph, which is wrapped by the first
    DIV</p>
     
    </div>
      <div>
        <p>This is a sample paragraph, which is wrapped by the
    second DIV</p>
      </div>
      <div>
       
    <p>This is a sample paragraph, which is wrapped by the third
    DIV</p>
      </div>
    </div>
    <div id="htmlcontainer"></div>
    <input type="button" value="Show function result" id="testbutton" />
    </body>
    </html>

    As you can see, the previous example shows how the $A function can be first used for retrieving an array of DIV elements, and then for traversing them. However, I'm pretty sure that you're asking: what's the difference between this and working with conventional HTML collections, like the ones returned by the popular "getElementByTagName()" method that comes with the DOM?

    Well, the answer is simple. As I said before, array objects expose a remarkable set of methods that can be used, among other things, to implement iterators, search and find array elements, count them, and much more.

    Nonetheless, some of these useful methods will be properly covered in the next article of the series. For now I'd like you to pay attention to the following example, which illustrates the use of the $H function. As you'll see below, this function is really helpful for turning an object into an hash, which can be accessed by using an associative array notation. That sounds pretty good, right?

    Okay, having explained basically what the $H function does, below I included another hands-on example to help you understand its functionality more clearly. Here it is: 

    <!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 of $H function</title>
    <script language="javascript" src="prototype-1.4.0.js"></script>
    <script language="javascript">
    // example using the $H function
    function converObjectToHash(){
       var obj={first: 1,second : 2,third: 3};
       var hash=$H(obj);
       var htmlcont=document.getElementById('htmlcontainer');
       htmlcont.innerHTML=hash['first']+' '+hash['second']+' '+hash
    ['third'];
    }
    window.onload=function(){
       if(document.getElementById && document.createElement &&
    document.getElementsByTagName){
         var btn=$('testbutton');
         if(!btn){return};
         btn.onclick=converObjectToHash;
       }
    }
    </script>
    </head>
    <body>
    <div id="htmlcontainer"></div>
    <input type="button" value="Show function result" id="testbutton" />
    </body>
    </html>

    In this case, I used the $H function for converting the above "obj" object into an enumerable hash, which can be accessed via an associative array syntax. Maybe you won't find this function as useful as other ones, but it 's worthwhile to notice that it can be used in conjunction with other methods that are covered profusely in Prototype's official site, located at http://www.prototype.conio.net.

    Besides, Sergio Pereira has published an excellent guide to Prototype on his web site, located at http://www.sergiopereira.com/articles/prototype.js.html. If you're interested in reading a detailed guide on this thorough JavaScript library, you may want to visit this site.

    All right, at this stage you hopefully learned the basics of how to use some handy functions bundled with Prototype, such as $, $A, and $H respectively. In the last section of this tutorial, I'm going to show you how to use another function, called $F, which is aimed at dealing specifically with the fields of online forms.

    To learn how this brand new function works, please click on the link below and read the next few lines.

    More JavaScript Articles
    More By Alejandro Gervasio


       · This first article of the series is focused on exploring the main features of this...
       · just a small correction - prototypes official site is...
       · Thank you commenting on this article and for the update on Prototype's official...
     

    JAVASCRIPT ARTICLES

    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...
    - 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...


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





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