JS: Popups | JavaScript

JavaScript Popups

alert Message Box

alert displays the message for end-user's information.

alert('PiTribe'); PiTribe
You may think of alert as MessageBox.Show(message, MessageBoxButtons.OK) in .Net Windows Application.

confirm OK Cancel box

confirm displays a message box to click one of two buttons either OK or Cancel. confirm returns Boolean data type:

var r = confirm('Hit OK for "Good", Cancel "Bad"'); if(r) console.log('Good'); else console.log('Bad'); Good

You will get Good if press OK

You may think of alert as MessageBox.Show(message, caption, MessageBoxButtons.OKCancel); .Net Windows Application

confirm cancel

If the user hit Cancel button on confirm box, confirm returns false.

var r = confirm('Hit OK for "Good", Cancel "Bad"'); if(r) console.log('Good'); else console.log('You chose Cancel'); Good

confirm OK

If the user hit OK button on confirm box, confirm returns true.

var r = confirm('Hit OK for "Good", Cancel "Bad"'); if(r) console.log('You chose OK'); else console.log('Bad'); Good

prompt input box

prompt asks user for input a value:

var r1 = prompt('Enter first value'); console.log(r1);

prompt default value

prompt allows to provide a defautl value in the second parameter:

var r2 = prompt('Enter the year', '2023'); console.log(r2); 2023

The user views 2023 as default value. If the user press enter of OK button, this value is returned by the prompt function, otherwise, returns the value whatever entered by the user.

prompt cancel

If the user hit Cancel button on the prompt box, prompt returns null, no matter what value entered in the input box:

var r = prompt('Enter any value in the input box and choose "Cancel" button.'); console.log(r); null