odbhut.shei.chhele
odbhut.shei.chhele

Reputation: 6224

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.

Upvotes: 3

Views: 8253

Answers (2)

user123_456
user123_456

Reputation: 5795

$(".alert").on('click', function(){
    e.preventDefault();
    alertify.confirm("Are you sure?", function (e) {
        if (e) {
            //window.location...redirect
        } else {
            //do something
        }
    });
});

You don't need to return true or false...you are checking it already in the if statement

Upvotes: -1

epascarello
epascarello

Reputation: 207501

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;
        }
    });
});

Upvotes: 8

Related Questions