Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.5 MB, 505 trang )
120
Part II Applying JavaScript
if (some condition) {
// do something here
}
Note The if statement is sometimes called the if conditional. I use these terms interchangeably
within this and other chapters to get you comfortable with both terms. But don’t confuse the if
conditional (the entire if statement) with the if condition, which is the Boolean expression that
the if statement evaluates.
The if statement examines the validity, or truthfulness, of a condition to determine whether
the code within the conditional (inside the braces) is to be executed. The condition is a
Boolean expression that, when evaluated to true, causes the if statement to execute the code
in the conditional. (You can negate an expression in the condition to cause the code to run
if the expression evaluates to false.) Recall the use of Boolean and unary operators from
Chapter 5, “Using Operators and Expressions.” Here’s an example:
if (! some condition) {
// do something here
}
In this case, the condition starts with the negation operator, which means that the condition
would need to evaluate to false for the code inside the conditional to execute.
The real-world airline cost example from earlier in the chapter might look like this in
pseudocode:
if (flightCost < 350) {
bookFlight();
}
If the flight costs less than $350, the code within the conditional executes. The garbage
example might look like this:
if (forecast != "windy") {
takeGarbageOut();
}
Later in this chapter, I show you how to use an else statement to cause other code to execute
when the condition is true.
You use if statement with many of the operators you learned about in Chapter 5, especially
relational operators that test whether one value is greater than or less than another value
and equality operators that test whether two values are equal to each other. Take a look at
these examples:
Chapter 6 Controlling Flow with Conditionals and Loops
121
var x = 4;
var y = 3;
// Equality test
if (x == y) {
// do something
}
Because the value in the variable x (4), does not equal the value in variable y (3), the code
within the if conditional (inside the braces) doesn’t execute. Here’s an example with a relational operator:
var x = 4;
var y = 3;
// Relational test
if (x > y) {
// do something
}
In this case, because the value in variable x (4) is greater than the value in variable y (3), the
code within the braces executes.
The next section shows an example that you can perform yourself. This example takes advantage of the prompt() function to get input from a visitor through a simple interface.
The prompt() Function in Internet Explorer
With the introduction of Windows Internet Explorer 7, the prompt() function is no longer
enabled by default. If you attempt to use the prompt() function with Internet Explorer, you
receive a security warning like the one shown in Figure 6-1, or possibly a page with the word
null, like the warning in Figure 6-2.
Figure 6-1 A security warning caused by the prompt() function in Internet Explorer.
Figure 6-2 When using the javascript: pseudo-protocol with the prompt() function, you can sometimes
receive a page with the word null.
122
Part II Applying JavaScript
You can reliably get around this feature by clicking the information bar (shown in Figure 6-1)
and selecting an option to allow scripts, or by changing the security settings. You can change
security settings in Internet Explorer, for example, by selecting Internet Options from the
Tools menu, clicking the Security tab, clicking Custom Level, and enabling the Allow Web
Sites To Prompt For Information Using Scripted Windows option within the Scripting section.
However, you can’t rely on your visitors doing the same with their Internet Explorer settings.
Therefore, the prompt() function is no longer as useful as it was before Internet Explorer 7
was introduced. Some programmers might argue that the prompt() function was annoying
(and I agree that it created problems sometimes), but it did have its advantages, and disabling
it does very little to enhance security. But sometime it’s useful for test purposes, such as in
the following exercise.
Using if to make decisions about program flow
1. Using Microsoft Visual Studio, Eclipse, or another editor, edit the ifexample.htm file in
the Chapter06 sample files folder, which you can find in the companion content.
2. In the page, replace the TO DO comment with the following code shown in boldface
type, (this code is also in the ifexample.txt file in the companion content):
"http://www.w3.org/TR/html4/strict.dtd">
This is an example from Chapter 6.
This is an example from Chapter 6.