Calin Manoli
Calin Manoli

Reputation: 33

Sweet Alert 2 and javascript:How do i make a Sweet alert button that sends me to a certain webpage when clicked

My code so far:

swal({ 
    title: 'Successfully Registered!', 
    text: "Do you want to go to the Log In page?", 
    type: 'success', 
    showCancelButton: true, 
    confirmButtonColor: '#3085d6', 
    cancelButtonColor: '#d33', 
    confirmButtonText: 'Yes, send me there!' 
    }, 
    function(){ 
       window.location.href= "http://example.com"; 
    }
); 

For some reason the window.location.href is not working and i tried using only location.href and if(isConfirm) etc.
What should i do?
JSFiddle:https://jsfiddle.net/2dz868kd/

Upvotes: 3

Views: 10543

Answers (2)

Limon Monte
Limon Monte

Reputation: 54439

Here's how you can make it with SweetAlert2:

Swal.fire({
    title: 'Successfully Registered!',
    text: 'Do you want to go to the Log In page?',
    icon: 'success',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes, send me there!'
 }).then(function(result) {
   if (result.isConfirmed) {
     location.assign("https://stackoverflow.com/")
   }
 });
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

SweetAlert2 documentation: https://sweetalert2.github.io/

Please note that SweetAlert2 and SweetAlert are two different projects. SweetAlert2 uses ES6 Promises.

Upvotes: 8

rohan parab
rohan parab

Reputation: 1795

This is what i use to redirect on click (OK)

<script>
sweetAlert({title: 'Error',text: 'Invalid OTP',type: 'error',onClose: function(){
        window.location.href = 'https://google.com';
}});
</script>

Upvotes: 0

Related Questions