The basic UI operations are
alert outputs a window with the message and stops execution until visitor presses “OK” button.
The message window is called a modal window. The word “modal” means that page interface is suspended until “OK” is clicked. A visitor can’t do anything until he clicks “OK”.
This method accepts two arguments:
result = prompt(text[, default]);
It outputs a modal window with the
text, OK/CANCEL buttons and input field.
prompt returns either a string (maybe empty) or
The result depends on user’s action. There are three options:
- If user types something into field and presses OK, then user text is the result.
- If user types nothing, but presses OK, then the result is
- If user presses CANCEL (or keyboard Escape), then result is
alert, the window is modal, user can’t do anything until he presses one of two buttons (or Escape which is same as CANCEL).
var years = prompt('How old are you?', 100) alert('You are ' + years + ' years old!')
Always set the default value in
prompt, otherwise IE will insert
"undefined" into dialog:
Try this in IE:
result = confirm(message)
confirm outputs the
message with two buttons: OK and CANCEL.
The result is
true/false, for OK/CANCEL.
var result = confirm("Should I say hello?") alert(result)
Note that you can’t style, decorate or change screen position of basic modal windows. That’s the main drawback of basic UI functions.
But all these functions are simple and require no additional code. That’s why people still use them.
alertoutputs a message.
promptoutputs a message and waits for user input, then returns the value or
nullif ESC is pressed.
confirmoutputs a message and awaits until user presses ok or cancel. The returned value is