Java Script Lesson 4 - Functions

JavaScript Lesson 4 ’ Functions

JavaScript can contain functions, which are useful tools that can be reused within a document. You may enter in your functions at the beginning of a document and call them up later, making it very easy to write your code.

First, we will begin with how to call a function. You may copy and paste this script into your own page to see how it will work.

lesson 4

On your page, you will see a button with Press Me and the message, Press this button to see what happens when this script is called. When you click on the button, a small window will appear with the text, This is a sample Alert Box.

We begin by declaring the type of script that will be used. In this case, we are using text/javascript.

The result of the window containing our text is achieved by first naming your function, in our example, we have called it Alertbox. Next to function alertbox in the above script, you will see two parentheses. () These are used because we do not have any arguments for this script. We will cover arguments in a later tutorial.

Next, you have what the alert will read: (“This is a sample Alert Box”). This is what will display when the button is pressed. You can modify this for whatever you would like your alert to say.

Then, you end this portion of the script with . The next phase of the script is your form, so that you can create the pressable button to display your alert box. The onclick statement is where you will need to have the action from pressing the button display the alert box you coded in above.

In our next tutorial we will go more into depth on functions and how they work, as well as covering adding arguments to your functions. This will give your scripts more power and increase the functionality of your web page.

Related Posts:
Java Script Lesson 6 - Functions With Two Arguments
Java Script Lesson 5 - More Functions
Java Script Lesson 3 - Arrays
Java Script Lesson 8 ‘ Using More Than One JavaScript on a Page
PHP Lesson 5 - PHP Functions

Posted on 12/27/05 1:47 AM

Be the first to comment!

Leave a Reply


SYNDICATION

    NEW! Blog Feed:
    Reviews Feed:
    News Feed:
    Resources Feed: