Techcss
Javascript_page_menu

JavaScript Dialog Boxes & Prompts

To pop dialogue box or alert box in client end(browser end), javascript is the only solution which facilitates web programmers to create dialogue boxes as alerts, that display a message on a small window until the user is responding it by clicking 'OK' button and somewhere 'YES' / 'NO' button on the dialog box. These boxes are used to alert the user with warning message, error message or just for decision making responds from user end.

JavaScript generally interacts with the user through the prompt() method. This method displays a small dialogue box with a provision for text entry along with the two buttons, OK and Cancel. This method returns the text entered in the box which can be stored in a variable to use later as desired.

The confirm() method is similar to the alert(), but displays two buttons OK and Cancel.


The alert() and prompt() method

Following example is showing the usage of the prompt() and alert() methods

Example:

<html>
<head>
<script>
	Var roll = prompt("Enter your Roll No", "99260");
	if (roll == 99260 || roll == "")
	{
		alert("Please enter your College Roll No.");
	}
	else
	{
		alert("Thank you");
	}

</script>
</head>

<body>

</body>
</html>
Output: JavaScript prompt alert

The Confirm() method

The usage of the confirm() method is accentuated through the following example.

<html>
<head>
<pre>
<script>
    var val1 = prompt("Enter the first number","xxx");
    var val2 = prompt("Enter the second number","xxx");	
    if (confirm("Do you confirm the data entered ?"))
    {
       value = val1 + val2;
       document.writeln("The concatenated value is " + value);
       var res = parseFloat(val1) + parseFloat(val2);
       var res1= parseInt(val1) + parseInt(val2);
       document.writeln("The converted float value is " + res);
       document.writeln("The converted integer value is " + res1);
     }
     else
     {
       alert("Do not miss your chances !!");
     }

</script>

</pre>
</head>

<body>

</body>
</html>
Output: JavaScript prompt alert