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
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
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
Old 11-09-2007, 10:41 AM   #2 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

Great tutorial, it just seems to be so darn easy.
maZtah is offline  
Reply With Quote
Old 11-09-2007, 01:29 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

It really is very easy and when you get comfortable with the structure of JavaScript and frameworks like Prototype you can produce code like the following (which is functionally the same as Karl's above).

javascript Code:
Event.observe(window, 'load', function() {
    $$("#myContainer > div").invoke("update", "I've changed!");
});

Last edited by Salathe : 11-09-2007 at 02:58 PM. Reason: spelling :(
Salathe is offline  
Reply With Quote
Old 11-14-2007, 10:29 AM   #4 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

That is so easy!
Haris is offline  
Reply With Quote
Old 11-05-2008, 12:52 PM   #5 (permalink)
The Visitor
 
Join Date: Nov 2008
Posts: 1
Thanks: 0
westworld is on a distinguished road
Default

you can also use dom:loaded
expl.
Code:
document.observe("dom:loaded", function() {
  // initially hide all containers for tab content
  $$('div.tabcontent').invoke('hide');
});
westworld can be found @ westworld
westworld is offline  
Reply With Quote
Reply



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 04:59 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