Alert Messages in Javascript

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:

  1. Alert box
  2. Confirm box
  3. 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:

Alert Dialog Demo

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 up’s request.

Syntax

window.alert(&quot;message&quot;);

Example for Confirm Box

&lt;!DOCTYPE html&gt;
&lt;head&gt;
&lt;meta charset=&quot;ISO-8859-1&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;script type=&quot;text/javascript&quot;&gt;
		confirm(&quot;Click a button&quot;);
	&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
  • <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:

Confirm Box Demo

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(&quot;text&quot;, default value&quot;);

Example for Prompt Box

&lt;!DOCTYPE html&gt;
&lt;head&gt;
&lt;meta charset=&quot;ISO-8859-1&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;script type=&quot;text/javascript&quot;&gt;
		var x = prompt(&quot;Hi!!!&quot;,&quot;please enter your name&quot;);
		alert(&quot;Nice to meet you :&quot; + x + &quot;!&quot;);
	&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

  • <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.

When the execution process is completed successfully we will get the following output:

Prompt Box Demo

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*