Home >Web Front-end >JS Tutorial >How to use confirm method
The confirm() method is used to pop up a dialog box with a specified message and "Confirm" and "Cancel" buttons. It is a built-in function in JavaScript, and the syntax is "confirm("specified content") "; If the visitor clicks the "OK" button, this method returns true, otherwise it returns false.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
confirm() method is used to pop up the confirmation dialog box.
The confirmation dialog box is mainly used to obtain the user's consent to any option. It displays a dialog box with two buttons: OK and Cancel. confirm can be used to implement the confirmation dialog box. Method, let’s look at the specific content below.
The confirm dialog box is created using JavaScript's built-in confirm() function. When the JavaScript confirm() function is triggered, a small box will pop up and display the "OK" button and the "Cancel" button.
If the user clicks the "OK" button, the window method confirm() will return true. If the user clicks the Cancel button, confirm() returns false.
Let’s look at a specific example
The code is as follows
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script type = "text/javascript"> <!-- function getConfirmation() { var retVal = confirm("Do you want to continue ?"); if( retVal == true ) { document.write ("User wants to continue!"); return true; } else { document.write ("User does not want to continue!"); return false; } } //--> </script> </head> <body> <form> <input type = "button" value = "Click Me" onclick = "getConfirmation();" /> </form> </body> </html>
The display effect on the browser is as follows
When clicking "Click Me", the following effect will be displayed
Clicking OK will display: "User wants to continue!"
Similarly , clicking Cancel will display: "User does not want to continue!"
This article ends here. For more exciting content, you can pay attention to php中文网 Column tutorial! ! !
The above is the detailed content of How to use confirm method. For more information, please follow other related articles on the PHP Chinese website!