JavaScript Window confirm() Method with Examples

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

The javascript confirm method enables developers to prompt users with a confirmation dialog, offering OK and Cancel options. This method is invaluable for scenarios where user consent is essential before executing a certain action, like deleting a record or submitting a form.

Confirm() Method in JavaScript

The Javascript confirm method belongs to the Window object in JavaScript, which represents the browser window or tab. The primary purpose of confirm in javascript is to prompt the user with a confirmation dialog having two options to choose from: OK or Cancel.

Work Flow:

  • Invoking confirm() displays a modal dialog box on top of the current page.
  • The dialog presents two buttons: OK and Cancel, along with a developer-specified message.
  • The user interacts by selecting an option.
  • Choosing OK results in the method returning true; selecting Cancel or closing the dialog without a choice returns false.

Syntax

The syntax for the confirm() method is:

Parameters

  • message (optional): The message or question to be displayed in the confirmation dialog.

Return Values

The Javascript confirm method returns a boolean value:

  • true if the user clicks OK.
  • false if the user clicks Cancel or closes the dialog without making a choice.

Examples

Example - 1: Confirmation Before Actions

HTML:

JavaScript (script.js):

Output:

output showing dialog box for confirmation before actions

output after accepting dialog box for confirmation before actions

output after rejecting dialog box for confirmation before actions

Explanation:

  • A basic HTML page contains a button labeled Delete File.
  • Clicking the button triggers an event listener.
  • The listener responds to button clicks, executing a function.
  • Within the function, the confirm in javascript method prompts the user to confirm file deletion.
  • The user's choice is stored in the userChoice variable.
  • Clicking OK triggers an alert stating File deleted!.
  • Choosing Cancel results in an alert stating Deletion cancelled.

Example - 2: Form Submission Validation

HTML:

JavaScript (script.js):

Output:

output showing dialog box for form submission validation

output after accepting dialog box for form submission validation

Explanation:

  • An HTML form with a username input and a submit button triggers an event when submitted.
  • This event listener intercepts form submissions and prevents default behavior for validation.
  • The confirm() method prompts the user to submit the form and records their choice in userChoice.
  • If the user selects OK, an alert confirms the successful submission, and the form is manually submitted. If Cancel is chosen, an alert indicates submission cancellation.

Example - 3: Subscription Cancellation Confirmation

HTML:

JavaScript (script.js):

Output:

output showing dialog box for subscription cancellation confirmation

output after accepting dialog box for subscription cancellation confirmation

Explanation:

  • A webpage has a button labeled Cancel Subscription.
  • Clicking the button triggers an event listener.
  • The listener watches for clicks on the Cancel Subscription button.
  • When clicked, confirm() prompts the user to confirm subscription cancellation.
  • If the user clicks OK, the script cancels the subscription and confirms with an alert.
  • If the user clicks Cancel, an alert appears indicating the cancellation was canceled.

Alternative Libraries for Custom Confirmation Dialogs:

  • SweetAlert2:
    A widely used library providing customizable pop-up dialogs for alerts, confirmations, and prompts.
  • Modal Libraries (e.g., Bootstrap, Materialize CSS):
    Offer versatile options for creating various types of custom dialogs.
  • Dialogflow (Google Cloud):
    Powerful platform for creating interactive chat-based applications with natural-sounding conversation flows.

Use Cases

1. Delete Confirmation:

  • Before permanently deleting a record, file, or data entry, it's important to confirm the user's intent.
  • The Javascript confirm method is used to ask the user if they are sure they want to proceed with the deletion.

2. Form Submission Validation:

  • In scenarios where a form contains sensitive information or requires special handling, using the confirm in JavaScript can be crucial.
  • It ensures that the user truly intends to submit the form, reducing the likelihood of unintentional data loss.

3. User Account Deactivation:

  • In situations where deactivating a user's account has significant consequences.
  • The Confirm in JavaScript method can be used to ensure that the user is aware of the action they are about to take.

4. Important Transactions:

  • For critical transactions like making a high-value purchase or initiating a funds transfer.
  • To ask the user for confirmation to avoid any irreversible actions.

5. Log Out Confirmation:

  • When a user attempts to log out of an application, using the confirm in javascript can provide a final check to ensure they want to proceed with the logout.

6. Preventing Accidental Page Navigation:

  • In scenarios where navigating away from a page might lead to loss of progress or data.
  • The confirm() can be used to provide a warning, allowing the user to reconsider.

Supported Browsers

BrowsersSupports
ChromeFull Support
EdgeFull Support
FirefoxFull support
SafariFull support
Node.jsFull support
DinoFull support

The Javascript confirm method is part of the Window object and is uniformly supported across all major web browsers, ensuring consistent behavior for users.

FAQs

Q. How can I customize the message in the confirmation dialog?

A. You can provide a custom message as an argument when calling the Javascript to confirm method, like this: confirm("Custom message goes here").

Q. What happens if a user clicks "OK" in the confirmation dialog?

A. If a user clicks OK, the confirm() method returns true, indicating that the user has confirmed the action.

Q. Is the confirm() method supported in all web browsers?

A. Yes, the confirm() method is supported in all major web browsers, ensuring consistent behavior across different platforms.

Q. Can I use the confirm() method without attaching it to an event?

A. Yes, you can use the confirm in JavaScript method without an event, but it's typically employed in response to user actions like button clicks or form submissions for effective interaction.

Q. What is the difference between alert() and confirm() in JavaScript?

A. alert() provides a way for the user to acknowledge any information. It displays a simple dialog box with the message and an "OK" button. confirm() is useful when you need the user to make a binary decision. It is used to display a dialog box with a message, along with "OK" and "Cancel" buttons.

Conclusion

  • The Confirm method is a member of the Window object and is used to prompt the user with a confirmation dialog, offering OK and Cancel options.
  • The syntax for the confirm in javascript is confirm(message), where message is an optional parameter allowing for a custom message in the confirmation dialog.
  • It returns true for OK and false for Cancel or no choice.
  • The Javascript confirm method finds common use in scenarios like deleting records, validating form submissions, and preventing accidental navigation away from pages with unsaved changes.
  • It is supported across all major web browsers, ensuring consistent behavior for users.