1

I am using swal alert box, I just need to close my current tab when the user clicks on ok button.

$(document).ready(function(){
    swal({
        title: "Are you sure ??",
        text: 'hii', 
        icon: "success",
        buttons: true,
        //dangerMode: true,
    })
    .then((willDelete) => {
      if (willDelete) {
        window.close();
      } else {
        window.close();
      }
    });
});
Akshay
  • 35
  • 8
  • What relation is there between the "current tab" and when the alert is shown? – freedomn-m Jun 13 '19 at 09:08
  • when page is fully loaded this swal alert is popup, when user click on ok button then current tab should be close – Akshay Jun 13 '19 at 09:11
  • You mean the browser window tab? See: https://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window – freedomn-m Jun 13 '19 at 09:16
  • yes browser window, but how i can use this in swal. – Akshay Jun 13 '19 at 09:21
  • You have two issues: how to close a browser-tab (see link above, short answer: you can't) and how to get value from swal: use `.then(..` https://sweetalert.js.org/guides/#using-promises – freedomn-m Jun 13 '19 at 09:23
  • see my updated code i using same, but getting warning. script may close only the windows that were opened by it – Akshay Jun 13 '19 at 09:25
  • Right, so now you have one issue (you shouldn't change the question as you get fixes, maybe add an update, but not change completely). How to close a window, which you can't: see https://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window – freedomn-m Jun 13 '19 at 09:27

1 Answers1

1

By using a customizing the confirm dialog from Docs You can use the following code below:

Swal.fire({
  title: "Error",
  text: "wrong user or password",
  type: "error",
  showCancelButton: false,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'OK'
}).then((result) => {
  if (result.value) {
    window.close();
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>

PEN: CLICK HERE

devmonster
  • 456
  • 6
  • 20