Domanda

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.

È stato utile?

Soluzione

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

Altri suggerimenti

$(".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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top