javascriptjquerydomalertify

Javascript Alertify wait for confirmation then go to another page


I am new to alertify.js. What I am trying to do is, when the user cicks on a link, the browser should show confirmation box. If the user clicks of then go to the next page. If the user clicks cancel then stay on the same page. Is it possible to do this using alertify.js?

Here is my HTML code.

<a href="index.php" class="alert">Logout</a>

Here is my JavaScript code.

$(".alert").on('click', function(){
    alertify.confirm("Are you sure?", function (e) {
        if (e) {
            return true;
        } else {
            return false;
        }
    });
});

But the problem is that whenever I click on the link, I go to the index.php page before clicking on the confirm.


Solution

  • The problem is it is impossible to make code wait. So what you need to do is cancel the original click and than call the code to navigate to the new page.

    $(".alert").on('click', function(e){
        e.preventDefault();
        var href = this.href;
        alertify.confirm("Are you sure?", function (e) {
            if (e) {
                window.location.href = href;
            }
        });
    });