confirm()
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!');}
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours