The onfirm () method is used to display a dialog box with the specified message and OK and cancel buttons. The cancel button is more than alert. If you click OK, confirm () returns true. If you click the cancel button, confirm () returns false. Before the user clicks the OK or cancel button to close the dialog box, it will block all user input to the browser.
When confirm () is called, the execution of JavaScript code is suspended. The next statement is not executed until the user responds.
The alert () method is not a prompt and has no other operations.
Confirm ()
The Code is as follows: |
Copy code |
<Html> <Head> <Title> usage of confrim </title> <Script type = "text/javascript"> Function clear1 () { If (confirm ("are you sure you want to clear the data? ")) { Document. main. text1.value = ""; } } </Script> </Head> <Boty> <Form name = "main"> <Input type = "text" name = "text1"/> <Input type = "button" name = "submit" value = "data clearing" onclick = "return clear1 ()"/> </Form> </Body> </Html> <Html> <Head> <Title> js confirm </title> <Script> Function begin () { Var a = confirm ("are Guo Yang and Xiao Dai good friends? "); If (a = true) { /* Document. write ("Congratulations! ");*/ Alert ("Congratulations! "); Begin (); } Else { /* Document. write ("You are really a pig. You have no answer to such a simple question! ");*/ Alert ("You are really a pig. You cannot answer such a simple question! "); Begin (); } } </Script> </Head> <Body onload = "begin ()"> </Body> </Html> |
Alert () method
Warning Message Box
The alert method has a parameter, that is, the text string to be displayed to the user. This string is not in HTML format. The message box provides a "OK" button for the user to close the message box, and the message box is a mode dialog box, that is, the user must close the message box before continuing the operation.
The Code is as follows: |
Copy code |
Window. alert ("Welcome! Click "OK" to continue. ");
|
Confirm message box
In the confirmation message box, you can ask the user a "yes-or-no" question. You can click "OK" or click "cancel. The Return Value of the confirm method is true or false. This message box is also a mode dialog box: You must close it in response to this dialog box (click a button) before proceeding to the next step.
The Code is as follows: |
Copy code |
Var truthBeTold = window. confirm ("Click OK to continue. Click Cancel to stop. "); If (truthBeTold ){ Window. alert ("welcome to our Web page! "); } Else window. alert ("goodbye! "); |
Instance
The Code is as follows: |
Copy code |
<Html> <Head> <Script type = "text/javascript"> Function message () { Alert ("this prompt box is called through the onload event. ") } </Script> </Head> <Body onload = "message ()"> </Body> </Html> |