Home > Article > Web Front-end > How to use confirm() method in JavaScript
#Usage of the confirm method: Create a prompt box with confirm and cancel buttons by setting the confirm function to the element. When confirmation is clicked, true is returned and cancel. Returns false
Today I will explain the specific use of the confirm() method in JavaScript. It is mainly used to display a dialog box with a specified message and OK and Cancel buttons. Next, I will explain it in detail in the article. Introduction
[Recommended courses:JavaScript course】
confirm(message)
message: refers to the plain text displayed in the pop-up dialog box
Usage:
When the user clicks the OK button, confirm() returns true; if the user clicks the cancel button, confirm() returns false.
It will block all user input to the browser until the OK button or Cancel button is clicked, that is, before the dialog box is closed. So during the process of calling confirm(), JavaScript will pause execution until the user responds
Example:
When we click the button, a dialog box will pop up, and when we click Confirm The page displays confirmation. When you click Cancel, the page displays Cancel
<script type="text/javascript"> function getConfirmation(){ var result= confirm("这是confirm对话框"); if( result== true ){ document.write("确定") return true; } else{ document.write("取消") return false; } } </script>
The rendering is as follows:
##Note:
Pay attention to the return problem during use, because confirm has a return value, and the onclick function requires a return value. If the confirm value is not returned, onclick cannot detect the return value, so onclick receives an undefined value.Summary: The above is all of this article The content is complete. Through this article, I hope everyone can have a certain understanding of the use of the confirm() method in JavaScriptThe above is the detailed content of How to use confirm() method in JavaScript. For more information, please follow other related articles on the PHP Chinese website!