javaScript alert, prompt, confirm box Example

JavaScript Popup boxes; Through this tutorial, i am going to show you javaScript alert popup and its uses.

Learn JavaScript alert, prompt, confirm Popup Box

Javascript popups are of three types: Alert Popup, Confirm Box Popup, Prompt Box Popup. When using pop-up box methods you do not need to write “window” prefix. as follows:

  • Alert Box JavaScript
  • Confirm Box JavaScript
  • Prompt Box JavaScript

Alert Box JavaScript

When you need information to access the user, an alert box is used. It disrupts the user’s activity. This is the reason why you need to use it carefully. When the alert box appears, you have to press the OK button to start your activity again.

Syntax Of Alert Box

window.alert("Alert Text!");
Example
alert("How are you?");

Confirm Box JavaScript

When you need to accept something from the user that we use A confirmation pop-up box. When the confirmation pop-up box is open and you will see the two buttons name close and ok. if you want to agree than click ok button and not agree to close.

Syntax Of Confirm Box

window.confirm("Confirmation Information");

Need to know that – If the “OK” button is clicked, the confirmation box returns “true” . if the “cancel” button is pressed return “false”

Example
var cnfirm = confirm("Confirm something!");
if (cnfirm == true) { 
   x = "OK was pressed";  
}  
else {
   x = "Cancel was pressed";  
}

Prompt Box JavaScript

When you need to enter some information before the user goes ahead Then we do use the prompt box. If the user wants to close the prompts box then he will have to press OK or Cancel button.

Syntax Of Prompt Box
window.prompt("Information Text","Default Text");
Example
function firstFunction() {
    var txt_alert;
    var person = prompt("Please enter your full name:", "Harry Potter");
    if (person  == null || person  == "") {
        txt_alert = "User cancelled the prompt.";
    } else {
        txt_alert = "Hello " + person + "! How are you today?"
    }
    document.getElementById("demo_test").innerHTML = txt_alert;
}

More JavaScript Tutorials

One Comment

  1. Mwaka Emmanuel said:

    It is so Awesome, I wish you to upload more about JavaScript for a biginner like me to understand more.
    Thanks so much.

    November 14, 2019
    Reply

Leave a Reply

Your email address will not be published.