The window. confirm method in javascript is very useful. A confirmation dialog box is displayed.
The reason why we bring up this dialog box may be that this operation is dangerous, so you need to confirm it. However, if "OK" is selected by default, this principle may be violated.
In addition, the buttons in the confirm dialog box are fixed in "OK" and "cancel. Sometimes it is not intuitive.
Therefore, you can use msgbox In vbscript to rewrite this behavior. The following is an example.
Copy codeThe Code is as follows: <% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = "WebApplication1. _ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Script>
Function deleteConfirm (msg)
{
Function window. confirm (str)
{
Str = str. replace (/\ '/g, "' & chr (39 )&'"). replace (/\ r \ n/g, "'& VBCrLf &'");
ExecScript ("n = msgbox ('" + str + "', 289, 'delete box')", "vbscript ");
Return (n = 1 );
}
Return window. confirm (msg );
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: Button ID = "deleteButton" runat = "server" OnClientClick = "javascript: return deleteConfirm ('Are you sure you want to delete it? ') "Text =" delete"
Onclick = "deleteButton_Click"/>
</Div>
</Form>
</Body>
</Html>
For details about the msgbox method, refer to the following introduction.
MsgBox Function
The message is displayed in the dialog box. Wait for the user to click the button and return a value indicating the button clicked by the user.
MsgBox (prompt [, buttons] [, title] [, helpfile, context])
Parameters
Prompt
It is a string expression displayed in the dialog box. The maximum length of a prompt is about 1024 characters, depending on the width of the character used. If the prompt contains multiple rows, you can use a combination of carriage return (Chr (13), line feed (Chr (10), or carriage return linefeed (Chr (13) between each row) & Chr (10) is used to separate rows.
Buttons
A numeric expression indicates the number and type of buttons displayed, the icon style used, the identifier of the default button, and the total value of the message box style. For values, see the "Settings" section. If this parameter is omitted, the default value of buttons is 0.
Title
The string expression displayed in the title bar of the dialog box. If title is omitted, the application name is displayed in the title bar.
Helpfile
String expression used to identify the Help file that provides context-related help in the dialog box. If helpfile is provided, context must be provided. Unavailable on a 16-bit system platform.
Context
A numeric expression used to identify the context number specified by the creator of the Help file to a help topic. If context is provided, helpfile must be provided. Unavailable on a 16-bit system platform.
Set
The buttons parameter can have the following values:
Constant |
Value |
Description |
VbOKOnly |
0 |
Show onlyOKButton. |
VbOKCancel |
1 |
DisplayOKAndCancelButton. |
VbAbortRetryIgnore |
2 |
DisplayGive up,RetryAndIgnoreButton. |
VbYesNoCancel |
3 |
DisplayYes,NoAndCancelButton. |
VbYesNo |
4 |
DisplayYesAndNoButton. |
VbRetryCancel |
5 |
DisplayRetryAndCancelButton. |
VbCritical |
16 |
DisplayCritical InformationIcon. |
VbQuestion |
32 |
DisplayWarning QueryIcon. |
VbExclamation |
48 |
DisplayWarning MessageIcon. |
VbInformation |
64 |
DisplayInformation MessageIcon. |
VbDefaultButton1 |
0 |
The first button is the default button. |
VbDefaultButton2 |
256 |
The second button is the default button. |
VbDefaultButton3 |
512 |
The third button is the default button. |
VbDefaultButton4 |
768 |
The fourth button is the default button. |
VbApplicationModal |
0 |
Application Mode: You must respond to the message box to continue working in the current application. |
VbSystemModal |
4096 |
System Mode: All applications are suspended before the user responds to the message box. |
The first value (0-5) is used to describe the type and number of buttons displayed in the dialog box. The second value (16, 32, 48, 64) is used to describe the icon style; the third value (0,256,512) is used to determine the default button, while the fourth value (0, 4096) determines the message frame style. When these numbers are used to generate the buttons parameter value, only one number can be used from each group of values.
Return Value
MsgBox functions return the following values:
Constant |
Value |
Button |
VbOK |
1 |
OK |
VbCancel |
2 |
Cancel |
VbAbort |
3 |
Give up |
VbRetry |
4 |
Retry |
VbIgnore |
5 |
Ignore |
VbYes |
6 |
Yes |
VbNo |
7 |
No |
Description
If both helpfile and context are provided, you can press F1 to view the help topic corresponding to the context.
If the cancel button is displayed in the dialog box, Press ESC and click Cancel. If the dialog box contains the Help button, context-related help is provided for the dialog box. However, no value is returned before other buttons are clicked.
When MicroSoft Internet Explorer uses the MsgBox function, the title of any Dialog Box always includes "VBScript" to distinguish it from the standard dialog box.
The following example demonstrates the usage of the MsgBox function:Copy codeThe Code is as follows: Dim MyVar
MyVar = MsgBox ("Hello World! ", 65," MsgBox Example ")
'Myvar contains either 1 or 2, depending on which button is clicked