confirm()

DaltonAC's avatar
Published Jun 17, 2023
Contribute to Docs

The confirm() method takes in a message parameter that will be displayed in a pop-up window at the current location. The user can select ‘cancel’ or ‘ok’ by default, this information is sent back to the browser as a boolean statement which determines what will happen based on the if statement when used.

Syntax

The confirm() method has one optional parameter which takes in a string to display a message.

window.confirm(message);

Example 1

The following example uses the confirm() function to ask the user if they wish to exit the page.

window.confirm('Do you want to exit?');

Example 2

The following example uses the confirm() function to ask the user if they wish to exit the page. It uses an if statement to control how the function performs based on the input. The passed in “exit.html” will redirect the user to that page if that is the desired outcome.

if (window.confirm('Do you really want to leave?')) {
window.open('exit.html', 'Thanks for Visiting!');
}

All contributors

Contribute to Docs

Learn JavaScript on Codecademy