HTML and CSS Reference
In-Depth Information
EXPLANATION
1
The JavaScript program starts here in the head of the document. The function is
defined within the head of the document to guarantee that it will be defined be-
fore being called.
2, 3
The function greetings() is defined. It is very simple. It causes the background col-
or of the document to be a light blue color and causes an alert box to appear with
a greeting message.
4
The href attribute of the link tag is assigned a string consisting of the JavaScript:
pseudoprotocol, followed by the name of the function to be called. When the user
clicks this link, JavaScript calls the function, greetings() . (See Figure 7.5.)
Figure 7.5 After clicking the link, the function is called, causing the alert dialog box
to appear.
Calling a Function from an Event. An event is triggered when a user performs
some action, like clicking a button or moving the mouse over a link. The function
assigned to the event is called an event handler. When the event is triggered, the func-
tion is called. In the following example, when the user clicks the Welcome button, the
function is called.
EXAMPLE 7.4
<html>
<head><title>Functions and Events</title>
<script type="text/javascript">
1
function greetings(you){ // Function definition
2
document.bgColor="lavender";
alert("Greetings and Salutations! " + you);
}
</script>
</head>
Continues
 
Search WWH ::




Custom Search