Java Script Lesson 5 - More Functions
JavaScript Lesson 5 ’ More Functions
In our last tutorial we began to cover functions and started with a simple, argument free example of adding a clickable button, which displays a text box upon calling the function. We are now ready to begin adding in arguments to our functions so that we can do more with our scripts.
You can copy and paste this script into your page to try it for yourself.

Since you are now familiar with how simple functions work, we have added an argument to our existing function. We start with declaring the script type that will be used, text/javascript. You then need to name your function so that it can be called later in the document. We have named our function alertbox2. We have also declared that the script is an alert box, by entering in alert(txt).
Now, you can continue with your document and call the function where you would like it to display on your page. You will need to specify what the input type is, as we have done with button. You then declare what will happen when the first button, named Greeting will display, by entering in your message. In our script, Hello! Will display in the alert box.
To add an argument, we have created a second button, which will display, In Closing. When this button is clicked, the words Goodbye will display in the alert box.
When you upload this page to your browser, you will see two buttons, one named Greeting and the other named In Closing. The text, Two Buttons will now display. Click on each to see the message, will display beneath the buttons.
Feel free to modify this text as you try it out on your website. You can change the names of the buttons, what will display when they are clicked and what will display on your page beneath the buttons.
This is another simple example of how you can add interest to your website by using functions with and without arguments. Now that you have the basics down, we can begin using more difficult and enhanced functions that will increase the visual appeal of your website and let your visitors have a little fun during their stay at your site.
Our next tutorial will cover how to use functions to return an answer from two different arguments.
Related Posts:Posted on 12/27/05 1:48 AM
Be the first to comment!