mobileNavlogo
headerlogo

Dialog Boxes in JavaScript

05 October, 2020

Ratings

Related Blogs

 

JavaScript Dialog Boxes

JavaScript has three kinds of popup boxes: Alert box, Confirm box, and Prompt box.  

Alert Box

An alert box is often used if you want to make sure information comes through to the user. When an alert box pops up, the user will have to click “ok” to proceed.

Syntax

alert(“some text”);  

Learn JavaScript by Tekslate - Fastest growing sector in the industry. Explore Online "JavaScript Training" and course is aligned with industry needs & developed by industry veterans. Tekslate will turn you into JavaScript Expert.

 

Example

<html>

<head>

<script type=”text/javascript”>

functionshow_alert() { alert(“I am an alert box :”) ;

}

</script>

</head>

<body>

<input type=”button”  onclick=”show_alert()”   value=”show alert box” />

</body>

</html>  

Confirm Box

A confirm box is often used if you want the user to verify or accept something. When a confirm box pops up, the user will have to click either “OK” or “cancel” to proceed. If the user clicks “OK”, the box returns true. If the user clicks “Cancel”, the box returns false.

Syntax

confirm(“some text”); 

Example

<html>

<head>

<script type=”text/javascript”>

functionshow_confirm()

{

var r=confirm(“Press a button”);

if (r==true)

{

alert(“you pressed OK!”);

}

else

{

alert(“you pressed cancel!”);

}

}

</script>

</head>

<body>

<input type=”button” onclick=”show_confirm()” value=”show confirm box” />

</body>

</html>  

Prompt Box

A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either “OK” or “Cancel” to proceed after entering an input value. If the user clicks “OK” the box returns the input value. If the user clicks “Cancel” the box returns null.

Syntax

prompt(“sometext”,”defaultvalue”);  

Example

<html>

<head>

<script type=”text/javascript”>

functionshow_prompt()

{

var name=prompt(“Please enter your name”,”Harry potter”);

if (name!=null  && name!=””)

{

document.write(“Hello ” + name + “ ! How are you today?”);

}

}

</script>

</head>

<body>

<input type=”button” onclick=”show_confirm()” value=”show prompt box” />

</body>

</html>
About Author
Authorlogo
Name

TekSlate

Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .