Alert and Confirm
Join the Discussion
In the case of the alert and confirm built-in functions the browser knows that when these functions are called that it has to display a dialog box. A dialog box is a pop-up window that appears on the screen and remains there until you select one of the buttons within it. Until you do so this popup window will remain in front of your browser window and refuse to allow you to access the main browser window.
The alert and confirm functions both expect one parameter. This parameter is the text that is to be displayed in the dialog box. The rest of the information on how the dialog box is to be displayed - title bar content, logo (if any) and general layout of the dialog box are under the control of the browser so the dialog box will appear differently in each different browser but will always display the specified text along with either an OK button (for an alert) or both an OK button and a Cancel button (for a confirm).
The alert built in function is useful for displaying a message on the screen that you don't want your visitors to miss. They will have to select the OK button to proceed.
Here is an example of an alert function call:
An alert can display whatever you want in the dialog box similar to how we have been displaying whatever we wanted to on the web page using the document.write statement. The only differnce between alert and document.write is that the alert displays the message in a dialog box instead of on the web page and the alert function doesn't understand HTML so there is no point in including HTML tags into an alert call.
The confirm function differs from the alert function only in that it has two buttons to choose between instead of only one. The confirm function returns true or false depending on which of the two buttons is selected. If OK is selected then the confirm function returns true, if Cancel is selected it returns false.
Here's an example of the confirm function:
alert('You selected OK');
else alert('You selected Cancel');
Using What You Know
At this point you can easily revisit any of the scripts that you have written based on what you learned in previous tutorials and replace the document.write statements with alert function calls.