29

I am using Sweet Alert for a popup on my product view in an E-commerce Application with two buttons: one for going on cart View and another for reloading the view.

But when a user clicks outside of the popup, the popup window closes automatically. I've tried following properties to stop it to be closed but nothing works :

hideOnOverlayClick: false,
hideOnContentClick: false,
closeClick: false,
helpers: {
    overlay: { closeClick: false } 
}

Any help/suggestion is highly appreciated.

Thanks.

fdehanne
  • 1,759
  • 1
  • 16
  • 27
Saqib A. Azhar
  • 804
  • 1
  • 13
  • 24

13 Answers13

58

If you are using Sweet Alert 2, you can use this configuration

allowOutsideClick: false

This should work.

SimonHawesome
  • 1,223
  • 2
  • 13
  • 18
Halawa
  • 789
  • 1
  • 7
  • 18
27

The property you are looking for is closeOnClickOutside:

closeOnClickOutside: false
Channel
  • 1,915
  • 18
  • 16
17

For SweetAlert 2:

allowOutsideClick: false

and version 3 and some below version 2:

closeOnClickOutside: false
Jack
  • 3,392
  • 7
  • 36
  • 61
SouravOrii
  • 505
  • 7
  • 13
  • 1
    the `closeOnClickOutside: false` works like charm for Vue wrapper for Sweet Alert by Antério Vieira (https://github.com/anteriovieira/vue-swal) – Akbar Noto Mar 05 '20 at 17:17
14

Regarding Sweet Alert 2 (More effective solution)

Actually all answers here don't cover another way to dismiss the popup. And that's using keyboard. Especially the ESC key. In order to prevent that you would want to add two options instead of one.

allowOutsideClick: false,
allowEscapeKey: false,

Quick example:

Swal.fire({
  title: 'Do not dismiss!',
  icon: 'warning',
  showConfirmButton: false,
  allowOutsideClick: false,
  allowEscapeKey: false
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
Ph0enixKM
  • 300
  • 2
  • 5
3

It is allowOutsideClick: false for example

swal({
  title: "View Cart",
  text: "Are you sure?",
  type: "warning",
  showCancelButton    : true,
  confirmButtonColor  : "#ff0000",
  confirmButtonText   : "Yes",
  allowOutsideClick: false,
  CancelButtonText    : "No"
            },
                function() //confirm
            {
                //if Yes do this
            }
);
Qammar Feroz
  • 677
  • 8
  • 19
  • this didn't work on new version (2.0+) of sweetalert . Although answer given by @Channel worked closeOnClickOutside: false – Irfan Raza Jun 22 '18 at 12:54
  • @Irfan Raza The difference in this approach is, if user click outside the dialog, it will confirm the user first, if he/she want to close it or not. Otherwise option in swal allowOutsideClick : false, is enough. – Qammar Feroz Jun 28 '18 at 08:57
3

We are using higher version than 2 for Sweat alert and in our case we needed.

for Chrome:

closeOnClickOutside: false

for Firefox:

allowOutsideClick: false 
3

I had the same problem and here's how I solved it: setCanceledOnTouchOutside(false)

var dialog = SweetAlertDialog(context, SweetAlertDialog.ERROR_TYPE);
dialog.setTitleText(getString(R.string.session_expired));
dialog.setConfirmClickListener { sDialog ->
    sDialog.dismissWithAnimation()
    Utils.signOut(context!!)
    Handler().postDelayed({
    startActivity(getLoginIntent(context!!))
    AcTrans.Builder(context!!).performFade()
    }, 500)};
dialog.setCanceledOnTouchOutside(false);
dialog.show();
Saqib A. Azhar
  • 804
  • 1
  • 13
  • 24
2

If the answers above do not work for you try:

closeOnClickOutside: false

Gareth32
  • 21
  • 6
2

Use backdrop:true with the allowOutsideClick: false as following below. It worked for me.

swal({
    backdrop:true,
    allowOutsideClick: false,
    title:'Warning!',
    text:'Do you want to delete records?',
    type:'warning',
    showCancelButton: 0,
    confirmButtonText: 'OK',
}).then(function(e) {
    if (e.value) {
        //do what you want
    }
})
gihandilanka
  • 374
  • 3
  • 14
1

For sweetalert version < 2

swal(
         "Records will be deleted permanently.",  //title
         "Do you want to delete records?",  //text
         "warning",  //icon
         {
              closeOnClickOutside: false, // prevent close on click anywhere/outside
              buttons: ["No", "Yes"], //with custom label
              dangerMode: true,
         }
    ).then(ok => {
         if (ok) {
              console.log("deleted")
         }
         else {
              console.log("not deleted")
         }
    })
Suresh Maurya
  • 710
  • 10
  • 26
1

For latest version it is

allowOutsideClick: false
Swapnil Ghone
  • 571
  • 6
  • 14
0

You can set this property:

allowOutsideClick: true
Vinod Bhavnani
  • 1,829
  • 1
  • 13
  • 17
0

If you wan't want to close dialog on esc or outside click below is working for me.!

swal({
  title: "Are you sure?",
  text: "You will not be able to recover this details!",
  icon: "warning",
  closeOnClickOutside: false,
  closeOnEsc: false,
  allowOutsideClick: false,
  buttons: [
    'No, cancel it!',
    'Yes, I am sure!'
  ],
  dangerMode: true,
})
M.A.K. Ripon
  • 1,561
  • 3
  • 24
  • 42
Jitendra
  • 1
  • 2