There are three browser-specific functions to interact with visitors: alert, prompt, and confirm. All these methods are modal - they pause script execution and don’t allow the visitor to interact with the rest of the page until the window has been dismissed.
The alert shows a message. The prompt shows a message asking the user to input text. It returns the text or, if Cancel button or Esc is clicked, null. The confirm shows a message and waits for the user to press "OK" or "Cancel". It returns true for OK and false for Cancel or Esc.
This shows a message and pauses script execution until the user presses "OK". For example:
The mini-window with the message is called a modal window. The word "modal" means that the visitor can’t interact with the rest of the page until they have dealt with the window.
The function prompt accepts two arguments:
- title: The text to show the visitor.
- default: An optional second parameter, the initial value for the input field.
result = prompt(title, [default]);
It shows a modal window with a text message, an input field for the visitor, and the buttons OK/Cancel. The visitor may type something in the prompt input field and press OK. Or they can cancel the input by pressing Cancel or hitting the Esc key.
The call to prompt returns the text from the input field or null if the input was canceled. For example:
var age = prompt('How old are you?', 50);
alert('You are ' + age + ' years old.');
result = confirm(question);
It shows a modal window with a question and two buttons: OK and Cancel. The result is true if OK is pressed and false otherwise. For example:
var isTeacher = confirm('Are you a teacher?');