javascript - SweetAlert confirm redirect - Stack Overflow

I've got this function to reset the game after the user presses delete and then presses okagain.

I've got this function to reset the game after the user presses delete and then presses ok again.

function confirmReset() {
  swal({   
          title: "Are you sure you want to reset your game?",   text: "You will not be able to recover your game!",
           type: "warning",   
           showCancelButton: true,   
           confirmButtonColor: "#DD6B55",   
           confirmButtonText: "Yes, delete it!",   
           closeOnConfirm: false 
         }, function(){   
          swal("Deleted!", "Your imaginary file has been deleted.", "success"), 
            function(){
              window.location.href = "includes/php/reset.php?naam=<?php echo $naam ?>";
            }
      });
}

it works if i do the window.location.href at the place were the second swal('deleted'... etc) is at , but if i use a second function after the user preses OK aswell it wont fire the window.location.href

I've got this function to reset the game after the user presses delete and then presses ok again.

function confirmReset() {
  swal({   
          title: "Are you sure you want to reset your game?",   text: "You will not be able to recover your game!",
           type: "warning",   
           showCancelButton: true,   
           confirmButtonColor: "#DD6B55",   
           confirmButtonText: "Yes, delete it!",   
           closeOnConfirm: false 
         }, function(){   
          swal("Deleted!", "Your imaginary file has been deleted.", "success"), 
            function(){
              window.location.href = "includes/php/reset.php?naam=<?php echo $naam ?>";
            }
      });
}

it works if i do the window.location.href at the place were the second swal('deleted'... etc) is at , but if i use a second function after the user preses OK aswell it wont fire the window.location.href

Share Improve this question asked Jun 20, 2016 at 11:16 StabDevStabDev 7413 gold badges7 silver badges17 bronze badges 4
  • Why not just use setTimeout and refresh page inside that to give time for second alert to show? – charlietfl Commented Jun 20, 2016 at 11:36
  • I tried that, it works. but it isn't pretty. If the user clicks the ok button too early or too late it looks messy. I just want the page to refresh / go to the other page when the user clicks on the second OK @charlietfl – StabDev Commented Jun 20, 2016 at 11:40
  • Isn't there something like onConfirm window.location.hrefetcetcetc ? @charlietfl – StabDev Commented Jun 20, 2016 at 11:44
  • Create a demo. Not clear why what you have wouldn't work – charlietfl Commented Jun 20, 2016 at 11:45
Add a ment  | 

2 Answers 2

Reset to default 2

Try this code :-

function confirmReset() {
  swal({   
          title: "Are you sure you want to reset your game?",   text: "You will not be able to recover your game!",
           type: "warning",   
           showCancelButton: true,   
           confirmButtonColor: "#DD6B55",   
           confirmButtonText: "Yes, delete it!",   
           closeOnConfirm: false 
         }, function(){   
            swal({
                title: "Deleted!",
                text: "Your imaginary file has been deleted.",
                type: "success",
                //timer: 3000
            }, 
            function(){
              window.location.href = "/";
            })
      });
}

By adding a conditional statement & .then, you can redirect on the "OK" Button, and no need to implement the timer. I use this particular method for API calls.

function confirmReset() {
      swal({   
              title: "Are you sure you want to reset your game?",   
              text: "You will not be able to recover your game!",
              type: "warning",   
              showCancelButton: true,   
              confirmButtonColor: "#DD6B55",   
              confirmButtonText: "Yes, delete it!",   
              closeOnConfirm: false 
           }).then((result) => {
                if (result.isConfirmed) {
                 Swal.fire(
                  'Deleted!',
                  'Continue to .....',
                  'success'
                 ).then(function() {
                window.location = "/";
               })      
        }
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745039480a4607725.html

相关推荐

  • javascript - SweetAlert confirm redirect - Stack Overflow

    I've got this function to reset the game after the user presses delete and then presses okagain.

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信