JS in confirm reveal three buttons "yes" "No" "Cancel"

Source: Internet
Author: User

JS in Confirm prompt three buttons "yes" "No" "Cancel"

Overloading Dom in confirm

window.confirm = function (str) {
Str=str.replace (/\ '/g, "' &chr (&)"). Replace (/\r\n|\n|\r/g, "' &VBCrLf& '");
ExecScript ("n = MsgBox ('" + str + "', 3, ' hint ')", "VBScript");
return (n);
}

Call

function Send () {

var v = confirm ("Hint?") \U000D is (Y): Yes \u000d No (N): no ");
if (v== ' 6 ') {
Alert (' Yes ');

} else if (v== ' 7 ') {
Alert (' No ');

} else {
Alert (' Cancel ');

}
}

--------------

<script language=vbscript>
function Go ()
Go=msgbox ("haha", 3)
End Function
</script>
<script language=javascript>
var A=go ()
if (a==6)
{
Alert ("You are pressing ' yes '")
}
if (a==7)
{
Alert ("You are pressing ' no '")
}
if (a==2)
{
Alert ("You are pressing ' Cancel '")
}
</script>

-------------

The reason we pop up this dialog box may be that the operation is dangerous, so the user needs to be sure. However, if you choose OK by default, this principle may be violated.
In addition, the buttons of the confirm dialog are fixed in the "OK" and "Cancel" two. There may be times when it's not intuitive.
So, consider using MsgBox in VBScript to rewrite this behavior. Here is an example


<%@ 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= "Server" 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 the 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
The string expression that is displayed in the dialog box as a message. The maximum length of the prompt is approximately 1024 characters, depending on the width of the character being used. If the prompt contains more than one row, the rows are separated by a carriage return (CHR (13)), a newline character (Chr (10)), or a combination of a carriage return line break (CHR & Chr (10)).
Buttons
A numeric expression that represents the sum of the number and type of display buttons specified, the icon style used, the identity of the default button, and the value of the message box style. See the Settings section for values. If omitted, the default value for buttons is 0.
Title
A string expression that appears 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 that identifies the Help file that provides context-sensitive Help for the dialog box. If HelpFile is provided, the context must be provided. Not available on 16-bit system platforms.
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. Not available on 16-bit system platforms.
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 the Yes , no , and cancel buttons are displayed.
vbYesNo 4 Displays the Yes and no buttons.
Vbretrycancel 5 Displays the retry and cancel buttons.
vbcritical 16 Displays the critical information icon.
Vbquestion 32 Displays a warning query icon.
Vbexclamation 48 Displays the 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 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 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) determines 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 values:

TD Width= "81%" > no
button
vbok 1 OK
vbcancel 2 cancel
vbabort 3 abort
vbretry 4 retry
vbignore 5 ignore
vbyes 6 Yes
vbno 7


Description
If both the HelpFile and the context are available, the user can press F1 to see the Help topic that corresponds to the context.

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

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

The following example demonstrates the use of the MsgBox function:


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

-------------------------

I believe a lot of people are fed up with alert, confirm, and recently doing projects with Bootstrap, by encapsulating a bootstrap-style message box.

The implementation is very simple, bootstrap itself with the modal modal box, looks good:) just wrap it up and use it.

Uncensored no truth, less say more do, on the code.

The project is an ASP. NET MVC architecture, convenient for global invocation, I add the following HTML directly to the Global layout page:

<!--system modal start-to-
<div id= "Ycf-alert" class= "modal" >
<div class= "Modal-dialog modal-sm" >
<div class= "Modal-content" >
<div class= "Modal-header" >
<button type= "button" class= "Close" data-dismiss= "modal" ><span aria-hidden= "true" >&times;</span ><span class= "Sr-only" >Close</span></button>

</div>
<div class= "Modal-body small" >
<p>[Message]</p>
</div>
<div class= "Modal-footer" >
<button type= "button" class= "btn btn-primary OK" data-dismiss= "modal" >[BtnOk]</button>
<button type= "button" class= "btn btn-default Cancel" data-dismiss= "modal" >[BtnCancel]</button>
</div>
</div>
</div>
</div>
<!--system modal end-

Not much to say, are bootstrap modal style, unfamiliar friends can check bootstrap css, brackets [...] Content will eventually be replaced with the display of our incoming parameters.

The JS package is as follows:

;
$ (function () {
Window. Modal = function () {
var reg = new RegExp ("\\[([^\\[\\]]*?) \ \] ", ' IgM ');
var ALR = $ ("#ycf-alert");
var ahtml = alr.html ();

Restores the modal HTML as it was closed for replace with the next call
var _init = function () {
Alr.on ("Hidden.bs.modal", function (e) {
$ (this). html (ahtml);
// });
//}();

/* HTML restore does not do in _init (), there will be a problem when repeated calls, directly inside the _alert/_confirm do * *


var _alert = function (options) {
Alr.html (ahtml); Restoration
Alr.find ('. Ok '). Removeclass (' btn-success '). addclass (' btn-primary ');
Alr.find ('. Cancel '). Hide ();
_dialog (options);

return {
On:function (callback) {
If (callback && callback instanceof Function) {
Alr.find ('. Ok '). Click (function () {callback (TRUE)});
}
}
};
};

var _confirm = function (options) {
Alr.html (ahtml); Restoration
Alr.find ('. Ok '). Removeclass (' Btn-primary '). addclass (' btn-success ');
Alr.find ('. Cancel '). Show ();
_dialog (options);

return {
On:function (callback) {
If (callback && callback instanceof Function) {
Alr.find ('. Ok '). Click (function () {callback (TRUE)});
Alr.find ('. Cancel '). Click (function () {callback (FALSE)});
}
}
};
};

var _dialog = function (options) {
var ops = {
Msg: "Prompt content",
Title: "Action Tip",
Btnok: "OK",
Btncl: "Cancel"
};

$.extend (OPS, options);

Console.log (ALR);

var html = alr.html (). replace (reg, function (node, key) {
return {
Title:ops.title,
Message:ops.msg,
BtnOk:ops.btnok,
BtnCancel:ops.btncl
}[key];
});

alr.html (HTML);
Alr.modal ({
WIDTH:500,
Backdrop: ' Static '
});
}

return {
Alert: _alert,
Confirm: _confirm
}

}();
});

Call Method:

Each of the four options is an optional parameter
Modal.alert (
{
Msg: ' Content ',
Title: ' Title ',
Btnok: ' OK ',
Btncl: ' Cancel '
});

If you need to add a callback function, add the following directly. On (function (e) {});
Click "OK" e:true
Click "Cancel" E:false
Modal.confirm (
{
Msg: "Do you want to remove the role?" "
})
. On (function (e) {
Alert ("Return result:" + e);
});

JS in confirm reveal three buttons "yes" "No" "Cancel"

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.