How to use Popup Box in JavaScript

In this article I have described about popup boxes used in JavaScript
  • 2277

JavaScript Popup\Dialog Box - Alert and Confirm Dialog Box

JavaScript provides the ability to pickup user input or small amount of text to user by using Popup box or Dialog Box.

Alert Dialog Box

The simplest way to direct small amounts of textual output to a browser's window is to use an alert box.

Syntax

alert("<message>");

Example

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<script type="text/javascript">

    function popup() {

        alert("Hello ! WELCOME TO MCN");

    }

</script>

</head>

<body>

<input type="button" onclick="popup()" value="Click me" />

</body>

</html>

OUTPUT

When we run this program and click on click button

alertbx2.jpg


Confirm Dialog Box

As the name suggests, the confirm dialog box serves as a technique for confirming user action.

Syntax

confirm("<message>");

Example

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title>Confirm dialog Box</title>

    <button onclick="myFunction()">CLICK</button>

<p id="VAR"></p>

<script type="text/javascript">

    function myFunction()

      {

        var a;

        var b = confirm("DO YOU NEED IT SOLUTION");

        if (b == true) {

            a = "WELCOME TO MCN SOLUTIONS";

        }

        else {

            a = "THANKS FOR VISITING";

        }

        document.getElementById("VAR").innerHTML = a;

    }

</script>

</head>

</html>

OUTPUT

When we run program

cnfrm2.jpg

when we click on ok then

cnf3.jpg

You may also want to read these related articles :

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2018 DotNetHeaven. All rights reserved.