HTML and CSS Reference
In-Depth Information
Here's an example of a function definition:
function showAlerts()
alert("Please click OK to continue.");
alert("Please click OK again.");
alert("Click OK for the last time to continue.");
The function can be called using the following statement:
Now, we could include the showAlerts() function call in a button as follows:
<input type="button" value="click to see alerts"
onclick="showAlerts();" />
When the user clicks the button, the showAlerts() function will be called, and the
three alert messages will appear, one after the other. Typically, function definitions are
placed in the <head> area of the XHTML document. This loads the function definition
code but it does not execute until it is called. This ensures that the function definition is
loaded and ready to use before the function is called.
In this Hands-On Practice you will edit the quantityif.html document to move the
prompting script into a function and call it with an onclick event handler.
Edit the quantityif.html document as follows. There are a few things to note. The script
has been moved into the <head> area and included in a function definition. The
document.write() methods have been changed to alert() methods and the messages
have been altered slightly. The document.write() methods will not work well after the
page has already been written, as is the case in this exercise. Also, there have been some
comments added to the end brackets for the if statement and the function definition.
These comments can help you keep track of the code blocks within the script. The
indentation of the code blocks also helps to identify which brackets begin and end
various statements.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" lang="en" xml:lang="en">
<title>JavaScript Practice</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
<!-- <![CDATA[
function promptQuantity()
var quantity;
Search WWH ::

Custom Search