mozilla
Your Search Results

    Window.prompt()

    The Window.prompt() displays a dialog with an optional message prompting the user to input some text.

    Syntax

    result = window.prompt(text, value);
    
    • result is a string containing the text entered by the user, or the value null.
    • text is a string of text to display to the user. This parameter is optional and can be omitted if there is nothing to show in the prompt window.
    • value is a string containing the default value displayed in the text input field. It is an optional parameter. Note that in Internet Explorer 7 and 8, if you do not provide this parameter, the string "undefined" is the default value.

    Example

    var sign = prompt("What's your sign?");
    
    if (sign.toLowerCase() == "scorpio") {
      alert("Wow! I'm a Scorpio too!");
    }
    
    // there are many way to use prompt feature
    var sign = window.prompt(); // open the blank prompt window
    var sign = prompt();       //  open the blank prompt window
    var sign = window.prompt('Are you feeling lucky'); // open the window with Text "Are you feeling lucky"
    var sign = window.prompt('Are you feeling lucky', 'sure'); // open the window with Text "Are you feeling lucky" and default value "sure"

    When the user clicks the OK button, text entered in the input field is returned. If the user clicks OK without entering any text, an empty string is returned. If the user clicks the Cancel button, this function returns null.

    FIXME: external image!The above prompt appears as follows (in Chrome on Windows 7):

    Notes

    A prompt dialog contains a single-line textbox, a Cancel button, and an OK button, and returns the (possibly empty) text the user entered into that textbox.

    The following text is shared between this article, DOM:window.confirm and DOM:window.alert Dialog boxes are modal windows; they prevent the user from accessing the rest of the program's interface until the dialog box is closed. For this reason, you should not overuse any function that creates a dialog box (or modal window).

    Please note that result is a string. That means you should sometimes cast the value given by the user. For example, if his answer should be a Number, you should cast the value to Number. var aNumber = Number(window.prompt("Type a number", "")); 

    Mozilla Chrome code (e.g. Firefox extensions) should use methods of nsIPromptService instead.

    In Safari, if the user clicks Cancel, the function returns an empty string. Therefore, it doesn't differentiate canceling from the user entering an empty string in the textbox.

    This function has no effect in the Modern UI/Metro version of Internet Explorer for Windows 8. It does not display a prompt to the user, and always returns undefined. It is not clear whether this is a bug or intended behavior. Desktop versions of IE do implement this function.

    Specification

    Specified in HTML5.

    See also

    Hide Sidebar