Message boxes are methods of the window object. Because the window object is at the top of the object hierarchy, you can but do not have to use the full object name:
window.alert("Success!");
is the same as:
alert("Success!");
Alert Message Box
The alert method has one argument, the string of text you want to display to the user. The message box provides an OK button so the user can close it and is modal (i.e. the user must close the message box before continuing).
window.alert("Welcome! Press OK to continue.");
|
Confirm Message Box
The confirm message box lets you ask the user a "yes-or-no" question, and gives the user the option of clicking either an OK button or a Cancel button. The confirm method returns either true or false. This message box is also modal: the user must respond to it (click a button), and thereby close it, before proceeding.
confirmed = window.confirm("Click OK to continue. Click Cancel to stop.");
if (confirmed) { window.alert("Yoy clicked OK"); } else
{
window.alert("You clicked Cancel");
}
|
To run the code, paste it into JavaScript Editor, and click the Execute button.
Prompt Message Box
The prompt message box provides a text field in which the user can type an answer in response to your prompt. This box has an OK button and a Cancel button. If you provide a second string argument, the prompt message box displays that second string in the text field, as the default response. Otherwise, the default text is "<undefined>".
Like the alert( ) and confirm( ) methods, prompt displays a modal message box. The user must close it before continuing.
response = window.prompt("Welcome!","Please enter your name");
document.write(response);
|
To run the code, paste it into JavaScript Editor, and click the Execute button.
Next |