- JavaScript Basics
- JavaScript Tutorial
- JavaScript: where to write
- JavaScript: how to display
- JavaScript: keywords
- JavaScript: comments
- JavaScript: variables
- JavaScript: operators
- JavaScript: data types
- JavaScript Conditional Statements
- JavaScript: if-else
- JavaScript: switch
- JavaScript: for loop
- JavaScript: while loop
- JavaScript: do-while loop
- JavaScript: break and continue
- JavaScript Popup Boxes
- JavaScript: alert box
- JavaScript: confirm box
- JavaScript: prompt box
- JavaScript Popular Topics
- JavaScript: functions
- JavaScript: innerHTML
- JavaScript: getElementById()
- JavaScript: getElementsByClassName()
- JavaScript: getElementsByName()
- JavaScript: getElementsByTagName()
- JavaScript: querySelector()
- JavaScript: querySelectorAll()
- JavaScript: document.write()
- JavaScript: console.log()
- JavaScript: boolean
- JavaScript: events
- JavaScript: Math object
- JavaScript: Math.random()
- JavaScript: Number()
- JavaScript: parseInt()
- JavaScript: parseFloat()
- JavaScript Arrays
- JavaScript: array
- JavaScript: find length of array
- JavaScript: add element at beginning
- JavaScript: add element at end
- JavaScript: remove first element
- JavaScript: remove last element
- JavaScript: get first index
- JavaScript: get last index
- JavaScript: reverse an array
- JavaScript: sort an array
- JavaScript: concatenate arrays
- JavaScript: join()
- JavaScript: toString()
- JavaScript: from()
- JavaScript: check if value exists
- JavaScript: check if array
- JavaScript: slice an array
- JavaScript: splice()
- JavaScript: find()
- JavaScript: findIndex()
- JavaScript: entries()
- JavaScript: every()
- JavaScript: fill()
- JavaScript: filter()
- JavaScript: forEach()
- JavaScript: map()
- JavaScript Strings
- JavaScript: string
- JavaScript: length of string
- JavaScript: convert to lowercase
- JavaScript: convert to uppercase
- JavaScript: string concatenation
- JavaScript: search()
- JavaScript: indexOf()
- JavaScript: search() vs. indexOf()
- JavaScript: match()
- JavaScript: match() vs. search()
- JavaScript: replace()
- JavaScript: toString()
- JavaScript: String()
- JavaScript: includes()
- JavaScript: substr()
- JavaScript: slice string
- JavaScript: charAt()
- JavaScript: repeat()
- JavaScript: split()
- JavaScript: charCodeAt()
- JavaScript: fromCharCode()
- JavaScript: startsWith()
- JavaScript: endsWith()
- JavaScript: trim()
- JavaScript: lastIndexOf()
- JavaScript Date and Time
- JavaScript: date and time
- JavaScript: Date()
- JavaScript: getFullYear()
- JavaScript: getMonth()
- JavaScript: getDate()
- JavaScript: getDay()
- JavaScript: getHours()
- JavaScript: getMinutes()
- JavaScript: getSeconds()
- JavaScript: getMilliseconds()
- JavaScript: getTime()
- JavaScript: getUTCFullYear()
- JavaScript: getUTCMonth()
- JavaScript: getUTCDate()
- JavaScript: getUTCDay()
- JavaScript: getUTCHours()
- JavaScript: getUTCMinutes()
- JavaScript: getUTCSeconds()
- JavaScript: getUTCMilliseconds()
- JavaScript: toDateString()
- JavaScript: toLocaleDateString()
- JavaScript: toLocaleTimeString()
- JavaScript: toLocaleString()
- JavaScript: toUTCString()
- JavaScript: getTimezoneOffset()
- JavaScript: toISOString()
- JavaScript Regular Expression
- JavaScript: regular expression
- JavaScript: RegEx . (dot)
- JavaScript: RegEx \w and \W
- JavaScript: RegEx \d and \D
- JavaScript: RegEx \s and \S
- JavaScript: RegEx \b and \B
- JavaScript: RegEx \0
- JavaScript: RegEx \n
- JavaScript: RegEx \xxx
- JavaScript: RegEx \xdd
- JavaScript: RegEx quantifiers
- JavaScript: RegEx test()
- JavaScript: RegEx lastIndex
- JavaScript: RegEx source
- JavaScript Programs
- JavaScript Programs
JavaScript confirm()/window.confirm(): Confirm Dialog Box
Are you sick of wondering if your website visitors intended to take that action or if they accidentally clicked the wrong button? Do you want to make it easy for users to confirm their selections before proceeding? Look no further than JavaScript's confirm() function!
You can create a popup window that asks the user to confirm or cancel their action with just a few lines of code, giving them the opportunity to double-check their decision and avoid mistakes. So let's start with a brief definition.
The JavaScript confirm() method is used to verify the user's activity regarding the confirm() popup or dialog box.
JavaScript confirm() syntax
The syntax of the confirm() method in JavaScript is:
confirm(message)
The confirm() method returns true if the user clicks OK; otherwise, it returns false.
JavaScript confirm() example
Consider the following code as an example demonstrating the confirm() function in JavaScript:
<!DOCTYPE html> <html> <body> <h2>The confirm() Method</h2> <button onclick="myFunction()">Click Me</button> <p id="myP"></p> <script> function myFunction() { var msg; x = confirm("Press OK or Cancel button."); if(x==true) { msg = "You pressed OK button"; } else { msg = "You pressed Cancel button"; } document.getElementById("myP").innerHTML = msg; } </script> </body> </html>
The confirm() Method
Click on the Click Me button, then press either the OK or Cancel button. Then JavaScript writes the text based on the button you've clicked. It is used to ask the user about something and then proceed further based on their choice in this general way:
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Ask</button> <script> function myFunction() { if(confirm("Press OK or Cancel button.")) { // If the user is OK with your confirmation box, do some work here } else { // Do some work here as an alternative to OK } } </script> </body> </html>
It should be noted that the confirm() function is the same as window.confirm(). It is because window is a global object. And in JavaScript, a method by default belongs to the window object.
Advantages of the confirm() function in JavaScript
- Simple to use Use the straightforward function confirm() to ask the user to confirm an action before moving forward.
- Prevents user errors: By asking users to confirm a potentially risky action before it is carried out, confirm() can stop users from making mistakes.
- Feedback: The confirm() return value can be used to inform the user about the action they have taken.
Disadvantages of the confirm() function in JavaScript
- User experience is less seamless when confirm() is used because it can disrupt the user's natural flow.
- Limited customizability: Applications that demand a particular look and feel may find it difficult to customize the confirm dialog box's appearance and behavior.
- Overused: If confirm() is used excessively, users may become irritated and lose faith in the application. It's crucial to only use confirm() when absolutely necessary.
- Not accessible: Because of accessibility problems, some users might find it difficult to use confirm() because screen readers can't read the dialog box's contents.
« Previous Tutorial Next Tutorial »