YouTip LogoYouTip

Met Win Prompt

## JavaScript Window prompt() Method The `prompt()` method of the `Window` interface displays a modal dialog box with an optional message prompting the user to input some text. It is a quick and simple way to capture user input directly from the browser. --- ## Definition and Usage When `prompt()` is called, it halts execution of the script and displays a system dialog box. The dialog contains: 1. A text message (the prompt). 2. An input text field. 3. An **OK** button. 4. A **Cancel** button. ### Return Value * **`string`**: If the user clicks **OK**, the method returns the text entered in the input field (which can be an empty string `""`). * **`null`**: If the user clicks **Cancel** or closes the dialog box without submitting, the method returns `null`. --- ## Syntax ```javascript let result = window.prompt(message, defaultText); ``` *(Note: The `window.` prefix is optional. You can call `prompt()` directly.)* ### Parameters | Parameter | Type | Required/Optional | Description | | :--- | :--- | :--- | :--- | | `message` | `string` | Optional | A string of plain text (not HTML) to display to the user. If omitted, nothing is shown in the prompt body. | | `defaultText` | `string` | Optional | A string containing the default value displayed in the text input field. | --- ## Browser Support The `prompt()` method is a standard feature of the Web APIs and is supported by all major modern browsers: * Google Chrome * Microsoft Edge * Mozilla Firefox * Apple Safari * Opera --- ## Code Example The following example demonstrates how to display a prompt dialog asking for the user's name. If a name is provided, it displays a personalized greeting on the webpage. ### HTML ```html

``` ### JavaScript ```javascript function greetUser() { // Prompt the user for their name, with "Harry Potter" as the default input let person = prompt("Please enter your name:", "Harry Potter"); // Check if the user clicked "OK" and did not leave the input empty if (person !== null && person !== "") { let message = "Hello " + person + "! How are you doing today?"; document.getElementById("greeting-display").innerHTML = message; } else if (person === "") { document.getElementById("greeting-display").innerHTML = "You didn't enter anything!"; } else { document.getElementById("greeting-display").innerHTML = "Action canceled."; } } ``` --- ## Important Considerations & Best Practices While `prompt()` is easy to use for quick prototyping or debugging, you should keep the following in mind for production environments: 1. **Synchronous Blocking**: The `prompt()` dialog is synchronous and modal. It blocks the browser thread, preventing the user from interacting with the rest of the page until the dialog is dismissed. 2. **Styling Limitations**: The appearance of the prompt dialog is determined by the operating system and browser. You cannot style it with CSS. 3. **User Experience (UX)**: Modern web development generally discourages the use of native dialogs like `alert()`, `confirm()`, and `prompt()` because they disrupt the user flow. For a better user experience, consider building custom modal dialogs using HTML, CSS, and JavaScript (or using the HTML `` element).
← Met Win ResizebyMet Win Print β†’