You are here: Reference > JavaScript > client-side > dialog > methods > prompt (window)

prompt method (window)

Browser support:
Displays a prompt dialog with a message, an input field, an OK and a Cancel button.
Script execution does not continue until the prompt method returns (the user presses the OK or the Cancel button or closes the prompt dialog).
  • To display a notification dialog, use the alert method.
  • To display a confirmation dialog, use the confirm method.

Syntax:

object.prompt ([message [, defaultValue]]);
You can find the related objects in the Supported by objects section below.

Parameters:

message
Optional. String that specifies the text for the message in the dialog box. Default is an empty string.
defaultValue
Optional. String that specifies the default value of the input field. If not specified, the input field will be empty in Firefox, Opera, Google Chrome and Safari, and will contain 'undefined' in Internet Explorer.

Return value:

If the prompt dialog was closed by the Cancel button or by the system Close button, the prompt method returns null in Internet Explorer, Firefox, Google Chrome and Safari, and returns undefined in Opera. Otherwise, the prompt method returns a string that represents the value of the input field at that time when the prompt dialog was closed.

Example HTML code 1:

This example illustrates the use of the prompt method:
<head>
    <script type="text/javascript">
        function GetName () {
            var answer = prompt ("What is your name?", "Anonymus");
                // undefined == null, but '' != null
            if (answer == null) {
                alert ("Your did not specify your name!");
            }
            else {
                alert ("Your name is: " + answer);
            }
        }
    </script>
</head>
<body>
    <button onclick="GetName ()">Specify your name!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content