HTML and CSS Reference
In-Depth Information
FAQ
What can I do when my JavaScript code doesn't seem to be working?
You can try the following debugging techniques:
Open the Error Console in Firefox to see if there are any errors. Common errors include
missing a semicolon at the end of a line, and typing errors in commands.
Use alert() to print variables to verify the contents. For instance, if you have a variable
named quantity, try alert(quantity); to see what is contained in the variable.
Ask a classmate to look at your code. It's difficult to edit your own code because you tend
to see what you think you wrote rather than what you actually wrote. It's easier to edit
someone else's code.
Try to explain your code to a classmate. Often, talking through the code will help you
uncover errors.
Verify that you are not using any JavaScript reserved words as variable names or function
names. See http://www.webreference.com/javascript/reference/core_ref for a list of
reserved words.
HANDS-ON PRACTICE 14.6
In this Hands-On Practice you will code the quantity example described earlier. The
user will be prompted for a quantity and must enter a quantity greater than 0. We will
assume that the user will enter a number. If the user enters a value of 0 or a negative
number, there will be an error message displayed. If the user enters a value greater than
0, a message will be displayed thanking the user for the order. To stay focused on this
task, we will use a prompt and will write messages to the document.
Open a text editor and enter the following. Notice that there are no semicolon charac-
ters ( ; ) after the brackets:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>JavaScript Practice</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Using JavaScript</h1>
<script type="text/javascript">
<!-- <![CDATA[
var quantity;
quantity = prompt("Type a quantity greater than 0");
if (quantity <= 0)
{
document.write("<p>Quantity is not greater than 0.</p>");
document.write("<p>Please refresh the web page.</p>");
} else {
document.write("<p>Quantity is greater than 0.</p>");
}
 
Search WWH ::




Custom Search