简体   繁体   中英

Redirection after sweetalert closes

I want to be redirected to another page after the sweetalert modal closed on timeout. I am using the following code. Modal is showing and it is closing after timer ends but new page is not being redirected.

$('#with-timer').on('click', function () {
    var timerInterval;
    swal({
      title: 'Auto close alert!',
      html: 'I will close in <strong>2</strong> seconds.',
      timer: 2000
    }).then(function (result) {
      if (result.dismiss === swal.DismissReason.timer) {
        window.location.href = "index.php";
      }
    });
  });

Any suggestion to solve this problem?

Thank you.

Try using replace() instead of href()

Change your code to

.then(function (result) {
      if (result.dismiss === swal.DismissReason.timer) {
        window.location.replace = "index.php";
      }

It looks like you're mixing sweetalert with sweetalert2. They have different APIs. It looks like sweetalert2 tells you the reason why an alert is dismissed, so I'll use it for this answer, though it uses swal.fire to open up an alert.

Here's a working example:

 $('#with-timer').on('click', function () { var timerInterval; swal.fire({ title: 'Auto close alert,': html. 'I will close in <strong>2</strong> seconds,': timer. 2000 }).then(function (result) { if (result.dismiss === swal.DismissReason.timer) { window.location:href = "https.//stackoverflow;com"; } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script> <button id="with-timer">Open Modal</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM