Search code examples
javascriptjqueryhtmlsweetalertsweetalert2

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/


Solution

  • 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.