close

How to redirect page after click on Ok button on sweet alert?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to redirect page after click on Ok button on sweet alert? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to redirect page after click on Ok button on sweet alert ?

  1. How to redirect page after click on Ok button on sweet alert ?

    to redirect page after click on Ok button on sweet alert If anyone needs help, this code is working!
    swal({ title: 'Request Delivered', text: 'You can continue with your search.', type: 'success' }).then(function() { window.location.href = "index2.php"; })

  2. How to redirect page after click on Ok button on sweet alert ?

    to redirect page after click on Ok button on sweet alert If anyone needs help, this code is working!
    swal({ title: 'Request Delivered', text: 'You can continue with your search.', type: 'success' }).then(function() { window.location.href = "index2.php"; })

Method 1

Just make use of JavaScript promises. Put the then method after swal function. We do not need to use timer features. For example:

swal({
    title: "Wow!",
    text: "Message!",
    type: "success"
}).then(function() {
    window.location = "redirectURL";
});

The promise method .then is used to wait until the user reads the information of modal window and decide which decision to make by clicking in one button. For example, Yes or No.

After the click, the Sweet Alert could redirect the user to another screen, call another Sweet Alert modal window with contains new and subsequent question, go to a external link, etc.

Again, we do not have to use timer because it is much better to control user action. The user could wait for the eternity or take action as a Thanos’ or Iron Man’s finger snap. 😜

With the use of promises, the code becomes shorter, clean and elegant. 😉

Method 2

If anyone needs help, this code is working!

swal({
          title: 'Request Delivered',
          text: 'You can continue with your search.',
          type: 'success'
        }).then(function() {
            window.location.href = "index2.php";
        })

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment