The typical rendering of the alert includes an icon indicating a warning, regardless of the meaning of the message being presented.
The basic syntax for alert() is
or for shorthand,
as the Window object can be assumed.
The string passed to any dialog like an alert may be either a variable or the result of an expression. If you pass another form of data, it should be coerced into a string. All of the following examples are valid uses of the alert() method:
An alert window is page modal, meaning that it must receive focus and be cleared before the user is allowed to continue activity with the page.
A good use of alert dialogs is for debugging messages. If you are ever in doubt of where a script is executing or what current variables are set at and you don’t want to use a debugger, you can use an alert to display useful debugging information.
The confirm() method for the window object creates a window that displays a message for a user to respond to by clicking either an OK button to agree with the message or a Cancel button to disagree with the message. A typical rendering is shown here.
The writing of the confirmation question may influence the usability of the dialog significantly. Many confirmation messages are best answered with a Yes or No button rather than OK or Cancel, as shown by the dialog at right.
The basic syntax of the confirm() method is
where string is any valid string variable, literal, or expression that eventually evaluates to a string value to be used as the confirmation question.
The confirm() method returns a Boolean value that indicates whether or not the information was confirmed, true if the OK button was clicked and false if the window was closed or the Cancel button was clicked. This value can be saved to a variable, like so
answer = confirm("Do you want to do this?");
or the method call itself can be used within any construct that uses a Boolean expression such as an if statement, like the one here:
if (confirm("Do you want ketchup on that?")) alert("Pour it on!"); else alert("Hold the ketchup.");
Like the alert() method, confirmation dialogs should be browser modal.
The next example shows how the alert and confirm can be used.
The prompt() method takes two arguments. The first is a string that displays the prompt value and the second is a default value to put in the prompt window. The method returns a string value that contains the value entered by the user in the prompt. The basic syntax is shown here:
resultvalue = window.prompt(prompt string, default value string);
The shorthand prompt() is almost always used instead of window.prompt() and occasionally programmers will use only a single value in the method.
result = prompt("What is your favorite color?");
However, in most browsers you should see that a value of undefined is placed in the prompt line. You should set the second parameter to an empty string to keep this from happening.
result = prompt("What is your favorite color?","");
It is important when using the prompt() method to understand what is returned. If the user clicks the Cancel button in the dialog or clicks the Close box, a value of null will be returned. It is always a good idea to check for this. Otherwise, a string value will be returned. Programmers should be careful to convert prompt values to the appropriate type using parseInt() or similar methods if they do not want a string value.
The format of these last three dialogs leaves a little to be desired. We’ll see that it is possible to create our own forms of these dialogs, and to do so we start first with creating our own windows.