HTML and CSS Reference
In-Depth Information
Arguments are received
as parameters by the
receiving function and
stored in local variables;
they will disappear
when the function ends.
Arguments are
passed to the
receiving function.
Calling Function
Receiving Function
Figure 7.3 In the analogy of the pocket calculator, you are the caller when you
press the buttons, and the internal functions inside the calculator are the receiver.
<head><title>Passing Arguments</title>
<script type="text/javascript">
function greetings(pal) { / "Birdman!" is stored in pal
alert("Greetings to you, " + pal );
<body background="birdman.jpg">
<script type="text/javascript">
greetings("Birdman!"); // Passing an argument
The function, greetings() , has one parameter, called pal . This parameter holds a value
that is sent to the function when it was called. The parameter name is any valid Java-
Script variable name. At this point, the function has been defined but not called.
The alert method will display the string, “Greetings to you, ” concatenated to the
value stored in pal ; in this example that value is “Birdman!” .
The JavaScript program is in the body of the document. It contains a function call
that will invoke a function defined in the head of the document.
The function greetings() is called with one argument, “Birdman!” . This argument
will be sent to the function, and assigned to the parameter, pal . If the function had
been called in the head of the document as it was in Example 7.1, the background
image would not appear until after the user clicks the OK button in the alert box
(see Figure 7.4), but in this example, the image was loaded before the function
was called.
Search WWH ::

Custom Search