user3431239
user3431239

Reputation: 267

javascript, confirm > alert > redirect in sequence

im using bootbox and bootstrap

im creating a form which ask user for the confirmation, if confirm = true, then alert message and then only redirect. i found the solution which using window.alert, but it's ugly.

Here's what I have (using Bootstrap and BootBox; jsFiddle):

HTML:

<a class="alert" href=#>Alert!</a>

JavaScript:

$(document).on("click", ".alert", function (e) {
    bootbox.confirm("Hello world!", function (result) {
        if (result) {
            bootbox.alert('User added');
            //window.alert('ok');   //dun wan to use this, because it's ugly                  
            window.open('http://google.com', '_self'); //fiddle can't run this line
        }
    });
});

but the fiddle cannot run the 'window.open'. u can try at your development tool.

the problem i face now is the page will direct to the new page WITHOUT waiting the user to response on the alert. how to solve it?

there are plenty of example/solution on internet on 'how to alert then redirect', i tried them but nothing is worked. my case here is different, it shows by confirmation > alert > navigate.

please advice, thank you.

Upvotes: 2

Views: 1859

Answers (2)

Justinas
Justinas

Reputation: 43451

Read documentation of bootbox. There is one line that you need: bootbox.alert(message, callback), so you pass callback function :

function(){window.open('http://google.com', '_self');}

Upvotes: 1

Satpal
Satpal

Reputation: 133403

You should use callbacks as per documentation

Syntax

bootbox.alert(str message, fn callback)

Example:

bootbox.alert('User added', function(){
    window.open('http://google.com', '_self');
});

Upvotes: 7

Related Questions