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

前端 未结 12 619
别跟我提以往
别跟我提以往 2020-12-10 01:15

I am able to display sweet alert after the page refresh but I have to click on Ok button which I am getting on sweet alert to redirect the page.Please help me in this.

相关标签:
12条回答
  • 2020-12-10 01:55

    To specify a callback function, you have to use an object as the first argument, and the callback function as the second argument.

    echo '<script>
        setTimeout(function() {
            swal({
                title: "Wow!",
                text: "Message!",
                type: "success"
            }, function() {
                window.location = "redirectURL";
            });
        }, 1000);
    </script>';
    
    0 讨论(0)
  • 2020-12-10 01:55

    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.

    0 讨论(0)
  • 2020-12-10 01:55

    setTimeout(function () { 
    swal({
      title: "Wow!",
      text: "Message!",
      type: "success",
      confirmButtonText: "OK"
    },
    function(isConfirm){
      if (isConfirm) {
        window.location.href = "//stackoverflow.com";
      }
    }); }, 1000);
    <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">


    Or you can use the build-in function timer, i.e.:

    swal({
      title: "Success!",
      text: "Redirecting in 2 seconds.",
      type: "success",
      timer: 2000,
      showConfirmButton: false
    }, function(){
          window.location.href = "//stackoverflow.com";
    });
    <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">

    0 讨论(0)
  • 2020-12-10 01:55

    I did it by using this code:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.js"></script>
            <script>
    
            $(".confirm").on('click',function(){
                window.location.href = "index.php";
            });
    
            </script>
    

    Thanks.

    0 讨论(0)
  • 2020-12-10 01:56

    I think this will help. It's same as given by Mr. Barmer. But I have enclosed this within php tags.

    Here it goes....

        <?php if(!empty($_GET['submitted'])):?>
            <script>
            setTimeout(function() {
                swal({
                    title: "Congratulaions!",
                    text: "Signed up successfully, now verify your mail",
                    type: "success",
                    confirmButtonText: "Ok"
                }, function() {
                    window.location = "index.php";
                }, 1000);
            });
            </script>   
        <?php endif;?>
    
    0 讨论(0)
  • 2020-12-10 01:58
    function confirmDetete(ctl, event) {
    
        debugger;
        event.preventDefault();
        var defaultAction = $(ctl).prop("href");
        swal({
            title: "Are you sure?",
            text: "You will  be able to add it back again!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes, delete it!",
            cancelButtonText: "Cancel",
            closeOnConfirm: false,
            closeOnCancel: false
        },
            function (isConfirm) {
                if (isConfirm) {
                    $.get(ctl);
                    swal({
                        title: "success",
                        text: "Deleted",
                        confirmButtonText: "ok",
                        allowOutsideClick: "true"
                    }, function () { window.location.href = ctl })
    
         // $("#signupform").submit();
                } else {
                    swal("Cancelled", "Is safe :)", "success");
    
                }
            });
    }
    
    0 讨论(0)
提交回复
热议问题