Rewrite the window.confirm behavior _javascript techniques in JavaScript

Source: Internet
Author: User
JavaScript Window.confirm This method is very useful, you can pop up a confirmation dialog box

The reason we pop up this dialog is because the operation is dangerous, so the user can confirm it. However, if you select OK by default, this principle may be violated.
In addition, the buttons on the confirm dialog are fixed to the two "OK" and "Cancel". It may not be intuitive at times.
So, consider using the MsgBox in VBScript to rewrite this behavior. Here is an example

Copy Code code 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 ">
<title></title>
<script>
function Deleteconfirm (msg)
{
function Window.confirm (str)
{
str= str.replace (/\ '/g, "' & Chr () &"). Replace (/\r\n/g, "' & VBCrLf & '");
Execscript ("n = MsgBox (' + str +" ', 289, ' delete box ') "," VBScript ");
return (n = = 1);
}
return window.confirm (msg);
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:button id= "DeleteButton" runat= "onclientclick=" Javascript:return deleteconfirm (' Are you sure you want to delete it? ') text= "Delete"
onclick= "Deletebutton_click"/>
</div>
</form>
</body>


For details on the MsgBox method, you can also refer to the following introduction

MsgBox function
Displays a message in a dialog box, waits for the user to click the button, and returns a value indicating the button the user clicked.
MsgBox (prompt[, buttons][, title][, HelpFile, context))
Parameters
Prompt
A string expression displayed as a message in a dialog box. The maximum length of a prompt is approximately 1024 characters, depending on the width of the character being used. If the prompt contains more than one row, each row can be separated by a carriage return (CHR (13)), a newline character (CHR (10)), or a combination of a carriage return line feed (CHR & Chr (10)).
Buttons
A numeric expression that specifies the number and type of display buttons, the icon style used, the identity of the default button, and the sum of the values of the message box style. For numeric values, see the "Settings" section. If omitted, the default value for buttons is 0.
Title
A string expression displayed in the title bar of the dialog box. If you omit title, the name of the application is displayed in the title bar.
Helpfile
A string expression identifying the Help file that provides context-sensitive Help for a dialog box. If HelpFile is provided, the context must be provided. is not available on the 16-bit system platform.
Context
A numeric expression that identifies the context number assigned to a Help topic by the author of the Help file. If the context is already provided, HelpFile must be provided. is not available on the 16-bit system platform.
Set up
The buttons parameter can have the following values:

Constants value Description
vbOKOnly 0 Only the OK button is displayed.
vbOKCancel 1 Displays the OK and cancel buttons.
Vbabortretryignore 2 Displays the discard , retry , and ignore buttons.
vbYesNoCancel 3 Displays Yes , no , and cancel buttons.
vbYesNo 4 Show Yes and no buttons.
Vbretrycancel 5 Displays the retry and cancel buttons.
vbcritical 16 Displays the critical information icon.
Vbquestion 32 Displays the warning query icon.
Vbexclamation 48 Displays a warning message icon.
vbinformation 64 Displays an informational message icon.
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: The user must respond to a message box in order to continue working in the current application.
Vbsystemmodal 4096 System mode: All applications are suspended until the user responds to the message box.

The first set of values (0-5) describes the type and number of buttons displayed in the dialog box; the second set of values (16, 32, 48, 64) is used to describe the style of the icon, the third set of values (0, 256, 512) is used to determine the default button, and the fourth set of values (0, 4096) determines the style of the message box. When these numbers are added to generate the buttons parameter values, only one number can be taken from each set of values.

return value
The MsgBox function has the following return value:

Constants value Button
vbOK 1 Are you sure
Vbcancel 2 Cancel
Vbabort 3 Give
Vbretry 4 Retry
Vbignore 5 Ignore
Vbyes 6 Is
Vbno 7 Whether

description
If both the HelpFile and the context are provided, the user can press the F1 key to see the help topic that corresponds to the contextual.

If the dialog box displays the Cancel button, pressing the ESC key is the same as clicking Cancel. If the dialog box contains Help buttons, there is context-sensitive Help for the dialog box. However, no values are returned until the other buttons are clicked.

When Microsoft Internet Explorer uses the MsgBox function, the title of any dialog box always contains "VBScript" so that it distinguishes it from the standard dialog box.

The following example illustrates the use of the MsgBox function:
Copy Code code as follows:

Dim MyVar
MyVar = MsgBox ("Hello world!", "MsgBox Example")
' MyVar contains either 1 or 2, depending on which button is clicked

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.