简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM