alert("This is an important message!");
causes the following dialog box to be presented to the user:
Other forms of dialog with the user include the confirm() method, which displays its argument message in a dialog box with both OK and Cancel buttons. With the script
you should see the following window:
Last, we could use the prompt() method to collect some data from the user. A prompt displays its argument message in a dialog box and allows the user to enter data into a text field, as illustrated by this example:
var answer = prompt("What is your favorite color?","");
A common form of output is achieved through the Document object. This object provides many ways to manipulate Web pages, the simplest of which are the write() and writeln() methods. The write() method writes its arguments to the current document. The writeln() method is identical except that it inserts a linebreak after writing the argument. For example:
document.write("This text is not followed by a linebreak. "); document.writeln("However this uses writeln()."); document.write("So a newline was inserted.");
The result of this example in a browser window can be seen in Figure 2-1.
In addition to write() and writeln(), the Document object provides powerful features for manipulation of HTML and XML via the Document Object Model. The DOM, which is covered primarily in Chapter 10, can be used to replace or insert text, change formatting characteristics, and write to or read from HTML forms.