11

Hello I have a code using sweetalert

swal("Good job!", "You clicked the button!", "success")

this code will pop-up a message and has a button okay, what I like to do is I want to refresh the page after I click the okay button.

Can I do that?

Fil
  • 8,225
  • 14
  • 59
  • 85

11 Answers11

32

You can try this, it works for me.

swal({
       title: "Good job", 
       text: "You clicked the button!", 
       type: "success"
     },
   function(){ 
       location.reload();
   }
);
Yoshioka
  • 804
  • 1
  • 7
  • 13
28

The answer from Yoshioka did not work for me, I did this and it worked perfectly:

swal({title: "Good job", text: "You clicked the button!", type: 
"success"}).then(function(){ 
   location.reload();
   }
);
Leo C
  • 281
  • 3
  • 3
15

Use the callback function...

Swal.fire({
  // Swal Setting's
}).then((result) => {
  // Reload the Page
  location.reload();
});
Jan Heil
  • 395
  • 4
  • 13
5

For Sweet Alert 2, this will work.

swal("Good job!", "You clicked the button!", "success").then(function(){
    location.reload();
});

As you can see migration guide

Sweet Alert 2 uses Promise

Rohit Dhiman
  • 2,691
  • 18
  • 33
5

You can check confirm by this:

swal({
  title: "Good job",
  text: "You clicked the button!",
  icon: "success",
  buttons: [
    'NO',
    'YES'
  ],
}).then(function(isConfirm) {
  if (isConfirm) {
    location.reload();
  } else {
    //if no clicked => do something else
  }
});
Rohallah Hatami
  • 525
  • 6
  • 12
5

I use sweet alert 2 and this works for me

swal("Good job!", "You clicked the button!","success").then( () => {
    location.href = 'somepage.html'
})

‘’’ The answers making use of location.reload() is going to trigger your form to attempt to resubmit over and over again, thats why you should use location.href instead.

DevCisse
  • 130
  • 2
  • 7
Kingston Fortune
  • 905
  • 8
  • 17
2

In Sweet Alert 2, there is callback function where you can implement your logic :

Swal.fire({
  title: 'Great job',
  text: "You clicked the button!",
  type: 'success',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes'
}).then((result) => {
   if(result){
     // Do Stuff here for success
     location.reload();
   }else{
    // something other stuff
   }

})
Rahul Gupta
  • 991
  • 4
  • 12
1

You can find the reference on SweetAlert2.

Swal.fire(
    {
        title: "Good job", 
        text: "You clicked the button!", 
        type: "success",
        showDenyButton: true,          // In case you want two scenarios 
        denyButtonText: 'ABC',
        showCancelButton: true,        // In case you want two scenarios 
        cancelButtonText:'XYZ'
    }
).then(function (result) {
    if (result.isConfirmed) {
        //You can add code here if user pressed ok button
    } else if (result.isDenied) {
        //You can add code here if user pressed deny button
    } else if(result.isDismissed) {
        //You can add code here if user pressed cancel button
    }
)
Fil
  • 8,225
  • 14
  • 59
  • 85
1

this worked for me.

Swal.fire('Deleted !!', data.message, 'success').then(() => {
    location.reload();
});
Elikill58
  • 4,050
  • 24
  • 23
  • 45
Abdulrahim
  • 11
  • 1
0
swal({
   title: "Process Completed",
   text: "Data Recorded successfully",
   buttons: true
}).then(function(){
   location.reload();
});
0

i solved it with this :

swal({title: "Updated!", text: "yourText", type: "success"},function() {
                                location.reload();
     }) // swal end 

Another way :

swal("Updated!","yourText","success",function() {
     location.reload();
}) // swal end
Ripon Uddin
  • 709
  • 3
  • 14
  • 29