TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 11-08-2007, 03:12 PM   #1 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Smile Part 2: Using the Prototype JavaScript Framework

As an extension to Wildhoney's introductory article on JavaScript, I have written this article to show how to achieve the same results from using Prototype. Please note that the code in this example could be compacted considerably using some of Prototypes advanced features. However, due to this being an introduction to Prototype, I have decided to go with a "more code, easier to read", approach.

Prototype, if you don't already know, is a popular JavaScript Framework. I have a slightly different style to Wildhoney, I'm going to go ahead and paste the complete source code with extensive comments to help you understand what's going on.

javascript Code:
<html>
<head>
    <title>TalkPHP.com - Part 2: Using Prototype</title>
    <script type="text/javascript" src="prototype.js"></script>
    <script type="text/javascript">

        // Updates the specified element.  This function is called for each child
        // element in the "myContainer" element
        function updateDiv(pElement)
        {
            // Change this elements inner HTML to "I've changed!" by using
            // prototypes handy update() function
            pElement.update("I've changed!");         
        }
   
        function changeDivs()
        {

            // Get a reference to the div with the id 'myContainer'.  This is
            // Prototype equivalent to getElementById(..);
            var pContainer = $('myContainer');

            // Use Prototype's childElements() function to return a list of valid
            // child nodes, this function should be cross-browser compatible
            // and should therefor weed out the text elements that IE picks up
            var aContainerChildren = pContainer.childElements();

            // For each of the containers child nodes we want to call the updateDiv
            // function.  For each call, updateDiv will be passed a reference of
            // the element as its first argument.
            aContainerChildren.each(updateDiv);
        }
       
        // This simply tells the page to call our "changeDivs"
        // when the windows load event is triggered
        Event.observe(window, 'load', changeDivs);
   
    </script>
</head>
<body>

<div id="myContainer">
    <div>Change me</div>
    <div>Modify me</div>
    <div>Alter me</div>
</div>
   
</body>
</html>

If you go ahead and run the code you'll see that we've achieved the exact same results that we got from Wildhoney's article. However, this time, we've achieved it using Prototype's excellent Framework. If you were to remove all the comments, you'll see the code is much easier to read and a lot more flexible.

For more information on any of the function used in this example you can head over to the Prototype website: http://www.prototypejs.org
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:51 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design