HTML and CSS Reference
In-Depth Information
Figure 7.4
Output from the
greetings()
function in Example 7.2.
Calling a Function from a Link.
A function can be called directly from a link, by
using the JavaScript pseudoprotocol,
JavaScript:
, instead of a normal URL. The
Java-
Script:
protocol and the function call are placed within quotes and assigned to the
href
attribute of the
<a>
tag. When the user clicks his or her mouse on the link, instead of
the program going to the URL of another page, a JavaScript function will be called.
EXAMPLE
7.3
<html>
<head><title>Functions</title>
1
<script type="text/javascript">
2
function greetings(){
// Function defined within <
head
> tags
document.bgColor="lightblue";
alert("Greetings to you!");
3
}
</script>
</head>
<body bgcolor="silver">
<div align="center">
4
<a href="JavaScript:greetings()"><big>Click here for
Salutations</big>
</a><br />
</div>
</body>
</html>