Alert message box in JavaScript is used for sending the message to the user or taking the input from the user for the further processing of pages. Alert messages are one of the key part of the JavaScript programming. There are three types of popup boxes:
- Alert box
- Confirm box
- Prompt box.
Alert Dialog Box in JavaScript
It is a simple window containing a message. It is used to display specified message with alert box. It can be used to make sure confirmation which comes from the user. It is supported in all major browsers.
Syntax
window.alert("text");
Example for Alert Dialog
<!DOCTYPE html> <head> <meta charset="ISO-8859-1"> <title>JS alert box</title> </head> <body> <script type="text/javascript"> alert("Welcome to JavaBeat!!!"); </script> </body> </html>
- <script type=”text/javascript”> tag is used to define client side script which uses attribute type for specifying MIME type.
- alert(“Welcome to JavaBeat!!!”); line displays alert box with specified message.
Alert Dialog Demo
- Save the file as JSalert_example.html in your system.
- Just open the file in the browser, you will see the below picture in the browser.
When the execution process is completed successfully we will get the following output:
Confirm Box in JavaScript
It displays dialog box with specified message with ok and cancel button. It returns true, if user clicks ok button or else returns false, if user clicks false button. It is often used when we want to verify or accept something. It gives choice to user, they can either click ok button to confirm or else they can click cancel button if not agree with pop ups request.
Syntax
window.alert("message");
Example for Confirm Box
<!DOCTYPE html> <head> <meta charset="ISO-8859-1"> </head> <body> <script type="text/javascript"> confirm("Click a button"); </script> </body> </html>
- <script type=”text/javascript”> tag is used to define client side script which uses attribute type for specifying MIME type.
- confirm(“Click a button”) line displays confirm box with ok and cancel button.
Confirm Box Demo
- Save the file as JSconfirm_example.html in your system.
- Just open the file in the browser, you will see the below picture in the browser.
When the execution process is completed successfully we will get the following output:
Prompt Box in JavaScript
It is dialog box which asks user to enter some text. Now days some web pages ask to enter your name, such thing can be done by using prompt dialog box. It is used to get data from the user which will display with ok and cancel button. If user press ok button then entered value will be set or if user presses cancel button, null value will be set or default value will be set for that argument.
Syntax
window.prompt("text", default value");
Example for Prompt Box
<!DOCTYPE html> <head> <meta charset="ISO-8859-1"> </head> <body> <script type="text/javascript"> var x = prompt("Hi!!!","please enter your name"); alert("Nice to meet you :" + x + "!"); </script> </body> </html>
- <script type=”text/javascript”> tag is used to define client side script which uses attribute type for specifying MIME type.
- var x = prompt(“Hi!!!”,”please enter your name”); line indicates user to enter name using prompt dialog box.
- alert(“Nice to meet you :” + x + “!”); line displays alert message as specified.
Prompt Box Demo
- Save the file as JSprompt_example.html in your system.
- Just open the file in the browser, you will see the below picture in the browser.