JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

Alert Messages in Javascript

April 10, 2014 by Krishna Srinivasan Leave a Comment

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

[code lang=”html”]
window.alert("text");
[/code]

Example for Alert Dialog

[code lang=”html”]
<!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>
[/code]

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

[code lang=”html”]
window.alert(&quot;message&quot;);
[/code]

Example for Confirm Box

[code lang=”html”]
&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;
[/code]

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

[code lang=”html”]
window.prompt(&quot;text&quot;, default value&quot;);

[/code]

Example for Prompt Box

[code lang=”html”]
&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;

[/code]

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

Filed Under: JavaScript Tagged With: JavaScript Tutorials

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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved