Usage and difference of confirm () and alert () methods in js

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.