Javascript Confirm popup Yes, No button instead of OK and Cancel

JavascriptHtmlButtonPopupConfirm

Javascript Problem Overview


Javascript Confirm popup, I want to show Yes, No button instead of OK and Cancel.

I have used this vbscript code:

<script language="javascript">
    function window.confirm(str) {
        execScript('n = msgbox("' + str + '","4132")', "vbscript");
        return (n == 6);
    }
</script>

this only works in IE, In FF and Chrome, it doesn't work.

Is there any workround to achieve this in Javascript?

I also want to change the title of popup like in IE 'Windows Internet Explorer' is shown, I want to show here my own application name.

Javascript Solutions


Solution 1 - Javascript

Unfortunately, there is no cross-browser support for opening a confirmation dialog that is not the default OK/Cancel pair. The solution you provided uses VBScript, which is only available in IE.

I would suggest using a Javascript library that can build a DOM-based dialog instead. Try Jquery UI: http://jqueryui.com/

Solution 2 - Javascript

The only way you can accomplish this in a cross-browser way is to use a framework like jQuery UI and create a custom Dialog:

jquery Dialog

It doesn't work in exactly the same way as the built-in confirm popup but you should be able to make it do what you want.

Solution 3 - Javascript

You can also use http://projectshadowlight.org/jquery-easy-confirm-dialog/ . It's very simple and easy to use. Just include jquery common library and one more file only:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/blitzer/jquery-ui.css" type="text/css" />
<script src="jquery.easy-confirm-dialog.js"></script>

Solution 4 - Javascript

You can't do this cross-browser with the confirm() function or similar. I highly suggest you use something like the jQuery UI dialog feature to create an HTML dialog box instead.

Solution 5 - Javascript

Have a look at http://bootboxjs.com/

Very easy to use:

 bootbox.confirm("Are you sure?", function(result) {
  Example.show("Confirm result: "+result);
});

Solution 6 - Javascript

The featured (but small and simple) library you can use is JSDialog: js.plus/products/jsdialog

Here is a sample for creating a dialog with Yes and No buttons:

JSDialog.showConfirmDialog(
	"Save document before it will be closed?\nIf you press `No` all unsaved changes will be lost.",
	function(result) {
		// check result here
	},
	"warning",
	"yes|no|cancel"
);

JS Dialog demo screenshot

Solution 7 - Javascript

you can use sweetalert.

import into your HTML:

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>

and to fire the alert:

Swal.fire({
  title: 'Do you want to do this?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, Do this!',
  cancelButtonText: 'No'
}).then((result) => {
  if (result.value) {
    Swal.fire(
      'Done!',
      'This has been done.',
      'success'
    )
  }
})

for more data visit sweetalert alert website

Solution 8 - Javascript

  1. You can download and upload below files on your site

  1. after that you can directly use below code

$.alerts.okButton = "yes"; $.alerts.cancelButton = "no";

in document.ready function.

Please try it will work.

Thanks

Solution 9 - Javascript

Sweetalert2 provides different type of dialogs. You may also try for this. https://sweetalert2.github.io/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMuhammad AkhtarView Question on Stackoverflow
Solution 1 - JavascriptjohnveyView Answer on Stackoverflow
Solution 2 - Javascriptuser7094View Answer on Stackoverflow
Solution 3 - JavascriptQuanView Answer on Stackoverflow
Solution 4 - JavascriptcletusView Answer on Stackoverflow
Solution 5 - JavascriptAndzej MaciusovicView Answer on Stackoverflow
Solution 6 - JavascriptDan SpiritView Answer on Stackoverflow
Solution 7 - JavascriptOhad CohenView Answer on Stackoverflow
Solution 8 - JavascriptReshma bhalekarView Answer on Stackoverflow
Solution 9 - JavascriptKalpesh PadhariyaView Answer on Stackoverflow