JavaScript
  Home arrow JavaScript arrow Page 2 - Introducing JavaScript Functions and Loops
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  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
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 JavaScript Functions and Loops
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2007-11-12

    Table of Contents:
  • Introducing JavaScript Functions and Loops
  • Putting the Fun in Your Functions
  • Loops
  • While 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


    Introducing JavaScript Functions and Loops - Putting the Fun in Your Functions


    (Page 2 of 4 )

    Before I show you how to write a function and call one, let's go over a few rules. First, you can place a function in the head or body section and even in an outside .js file. You can call the function from any place within your page, and even from other pages in the event that you have stored an external .js file.

    As stated before, functions hold reusable code that users call by performing an action. Take a look at this example:


    <html>

    <head>


    <script type="text/javascript">

    function firstfunction() //This is how you declare a function

    {

    alert("Look at me! I'm a function!")

    }

    </script>

    </head>

    <body>

    <form>

    <input type="button"

    onclick="firstfunction()" //clicking on the button calls the function

    value="Click for Function">

    </form>

    </body>

    </html>

    In the above example, we wrote a program to create a pop-up button that then declares: Look at me! I'm a function! Of course the button isn't really a function, more the result of one, but you get the drift. Had we not enclosed our code in the function, it would execute when the page loaded, and that's it. Unless the user refreshed the page, it would not happen again.

    Instead, however, we did create a function, and we made a button for the user to click and call the function as often as they would like. As you can see by the above code, the way to declare a function is to type function nameoffunction(), followed by


    {

    your code

    }

    Note that if you are going to use variables for your function, and those variables are to be used only in that function, you must declare them like so:


    function nameoffunction(variable_one, variable_two, etc)

    Also note that the word “function” must be in lowercase letters and that the function's name is case-sensitive.

    Here is an example of a function that passes a value to a variable and then uses that variable.


    <html>

    <head>

    <script type="text/javascript">

    function varfunction(complex) //a function with a var named complex

    {

    alert(complex)

    }

    </script>

    </head>

    <body>

    <form>

    <input type="button"

    onclick=varfunction(2+1) //assigns value to var complex

    value="Complex Math: What is 2+1?"

    </form>

    </body>

    </html>

    The above code creates a function named varfunction and a variable named complex. It then creates a button that assigns a value to the variable complex and calls the function.

    The value of the variable in this case is the equation 2+1. When the user clicks the button, a pop-up appears that gives the result of 2+1, which of course is 3.

    We can of course have the function trigger when the page loads if we want. Maybe you have certain text that you wish to appear across multiple pages. While you would have to create a .js file for that, here is how you would write a function that would return some text on a page when the page loads:


    <html>

    <head>


    <script type="text/javascript">

    function myFunction()

    {

    return ("Harry Potter Fan Fiction is for Nerds.")

    }

    </script>

    </head>

    <body>


    <script type="text/javascript">

    document.write(myFunction())


    </script>

    </body>

    </html>

    Here we have placed the JavaScript in the head section of our page. We have also used a new statement, the Return statement. The Return statement specifies the value a function is to return. The above code stores the value “Harry Potter Fan Fiction is for Nerds.” It then prints the value to the web page.

    More JavaScript Articles
    More By James Payne


       · Thanks for dropping by for my discussion on Javascript functions and loops. Here we...
     

    JAVASCRIPT ARTICLES

    - 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
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway