JavaScript Window.prompt() Method

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

The JavaScript window.prompt() is a built-in function that is used to display a dialog box that asks the user to input some text. This pop-up box will appear and takes user input before entering the web page. It waits until the user enters any value in the text box or we can also cancel the dialog box. After entering the input, the web page loads fully. We can also use the prompt() method individually, without the "window" prefix. The user needs to click on "OK" or "Cancel" to cancel the prompt box.

Syntax of JavaScript Window.prompt() Method

The syntax of the prompt() in the javascript method is as follow:

In the above example of prompt() in javascript, text value is the text that we need to display in our dialog box. And the default is the string that will contain the value entered by the user as input.

Parameters of JavaScript Window.prompt() method

Following are the parameters of the window.prompt() method:

  • message: it is used to define the text message that we want to display in our dialog box. This can be left empty if we do not want to display any text in the dialog box. "Message" is an optional parameter.
  • default is a string value that contains the default input text. It is also an optional parameter. If this parameter is not provided, then the default value will be "undefined". It is also an optional parameter.

Return Value of JavaScript Window.prompt() Method

The return value of the window.prompt() method is a "string". The value entered by the user will be the default value of this method. If left empty, the default value will be "null".

Exceptions of JavaScript Window.prompt() Method

There are some exceptions that we need to take care of during the use of the window.prompt() method.

  • When the prompt box displays a pop-up window before landing on the main page, the user is restricted from seeing the contents of the main page because dialog boxes are modal windows. (Modal windows are those windows that require some action before entering the main page)
  • Sometimes when the user input is not a string, we need to perform casting of the input. suppose the user input is a number, then syntax will be like this-
  • After chrome version 46.0, prompt() method is prevented from being done inside an "iframe". This can be used only if there is an allow-modal value assigned to the method's sandbox attribute.
  • It may possible that the dialog box will not appear when the user is switching the tabs or it will not wait for the user to do some activity.

Example(simple and short example)

Let us see an example to understand the window.prompt() method.

Explanation: In the above example of prompt() in the javascript method, we have shown a simple prompt where a popup will come on the screen and asks the user to enter the name to proceed further. If not entered any value, the default text "name" will be accepted as user input.

Output:

How does JavaScript Window.prompt() Method work?

The window.prompt() method is used to show a dialog box before landing on the main web page. The "prompt() syntax is used inside the 'script' element of an HTML page. When the code will sun, a dialog box appears as a 'pop-up' on the screen. The user needs to enter some text or we can simply click the 'OK' or 'Cancel' button. The dialog prompt will not allow us to see the main page until we do any activity on the dialog box. The dialog prompt waits for the user to input the value or cancel it.

More Examples

Let us see some more examples for a better understanding of window.prompt() methods:

Example 1. Given below is an example of prompt() in javascriptmethod where two buttons are used and a text field to enter some text by the user.

Output:

  • Before clicking:

example-of-prompt-before-clicking

  • After clicking:

example-of-prompt-after-clicking

Example 2. Here is another example of a window.prompt() method where some HTML customization has been applied.

Output:

  • Before clicking:

example-of-window-prompt-before-clicking

  • On clicking:

example-of-window-prompt-on-clicking

  • After entering the text:

example-of-window-prompt-after-entering-text

Supported Browser

Following are the browsers that support the window.prompt() method:

supported-browser-window-prompt

Browser NameSupported on versions
Internet Explorerversion >= 6(Aug 2001)
Mozila Firefoxversion >= 2(Oct 2006)
Microsoft Edgeversion >= 12(Jul 2015)
Google Chromeversion >= 4(Jan 2010)
Safariversion >= 3.1(Mar 2008)

Conclusion:

  • window.prompt() method in JavaScript is used to display a dialog box that prompts the user to do some activity like entering a value, or pressing the "OK", or "Cancel" button.
  • If users do not do this, they will not be allowed to see the contents of the main page.
  • In prompt() in the javascript method we can also store the entered value in a variable and also write them on the web page as per need.
  • The prompt() method can also take a number input, but we need to do typecast of the data-type of input.
  • The prompt() in the javascript method is written inside the script element of the HTML page.
  • The default value in this method is an optional parameter.