Input and output are two essential components of any program. Although there have been some brief explanations with previous examples, the methods have not been formally introduced. The first is the write() method.
Write() is a method of the document object and, as such, it operates on HTML documents. The write method does not write text to the screen, but to the HTML that produces the Web page. Therefore, any arguments sent to the write() method must be HTML formatted. The two most used tags will likely be <br> and <p>. <br> is a line break, which moves text down to the next line. <p>, on the other hand, is a paragraph tag, and is equivalent to two line breaks.
The writeln() method is the same as the write() method, except that it appends a carriage return on the end of the string. The carriage return does not affect the onscreen representation of the method, but changes the way the HTML is formatted.
Unless you are writing into a <pre> block (preformatted text block), the only difference the end user might notice is if they choose the View Source option from within the browser (and the browser displays the WYSIWYG HTML source, not the JavaScript). If you use the write() method exclusively, the HTML will be one long line of uninterrupted text.
Another useful method is the alert() method, which produces a pop-up dialog box. The dialog box contains a graphical exclamation mark, the phrase 'JavaScript alert!', the string supplied as an argument, and an OK button. The alert() method is most useful to convey important information that doesn't require anything other than acknowledgement from the user.
Sometimes, however, you might need to ask for the user's acceptance before proceeding with an operation. In this case, the confirm() method is most useful. The confirm() method presents the same information as the alert button, with a few exceptions: the phrase 'JavaScript confirm:' appears instead of 'JavaScript alert!', a graphical question mark replaces the exclamation mark, and a Cancel button is added.
The confirm() method will return either true, if the OK button is pressed, or null, if the Cancel button is pressed.
Finally, a program will often require the user to enter information into the system. One of the easiest ways to get user input is with the prompt() method. The prompt() method presents the user with a pop-up dialog box containing the string sent as an argument, a text box, an OK button, and a Cancel button. The string the user supplied in the text box will be returned if they press OK, or null if the Cancel button is pressed. Finally, unlike the other methods, the prompt() method receives two arguments; the first argument is the text to be displayed to instruct the user, and the second argument is the default text that appears in the text box.
The syntax for the I/O functions is:
Syntax: document.write(HTMLString); document.writeln(HTMLString); alert(alertString); returnValue = confirm(confirmString); returnValue = prompt(question, default); |