HTML and CSS Reference
In-Depth Information
Figure 4.1 Using the alert() method with Firefox (left) and Internet Explorer (right).
EXAMPLE 4.2
<html>
<head>
<title>Using JavaScript alert box</title>
1
<script type="text/javascript">
2
var message1="Match your Quotes and ";
var message2="Beware of Little Bugs ";
3
alert("Welcome to\nJavaScript Programming!");
4
alert(message1 + message2);
</script
</head>
</html>
EXPLANATION
1
The JavaScript program starts here with the <script> tag.
2
Two variables, message1 and message2 are assigned text strings.
3
The alert() method contains a string of text. Buried in the string is a backslash se-
quence, \n . There are a number of these sequences available in JavaScript (see
Table 3.1 on page 54). The \n causes a line break between the two strings. The rea-
son for using the \n escape sequence is because HTML tags such as <br> are not
allowed in this dialog box. After the alert dialog box appears on the screen, the
program will stop until the user clicks the OK button.
4
The alert() method not only accepts literal strings of text, but also variables as ar-
guments. The + sign is used to concatenate the values of the two strings together
and create one string. That string will appear in the alert dialog box as shown in
the output in Figure 4.2.
Search WWH ::




Custom Search