admin 管理员组

文章数量: 1086019

I have a function called showModalConfirmDialog that creates a custom javascript made dialog box with two buttons Yes/No and dims the background. Now in my functions I want to call that function like:

var oute = showModalConfirmDialog('Are you sure?');

and I want to react depending on the button clicked;

if(oute == true){
    // do something
} else {
    // do something else
}

The buttons return true/false. Javascript code:

button1.onclick = function(evt){
    return true;
};

button2.onclick = function(evt){
    return false;
};

I don't know what I am missing, any help would be appreciated. Thanks

I have a function called showModalConfirmDialog that creates a custom javascript made dialog box with two buttons Yes/No and dims the background. Now in my functions I want to call that function like:

var oute = showModalConfirmDialog('Are you sure?');

and I want to react depending on the button clicked;

if(oute == true){
    // do something
} else {
    // do something else
}

The buttons return true/false. Javascript code:

button1.onclick = function(evt){
    return true;
};

button2.onclick = function(evt){
    return false;
};

I don't know what I am missing, any help would be appreciated. Thanks

Share Improve this question asked Nov 14, 2013 at 12:32 Shahe MasoyanShahe Masoyan 1774 gold badges4 silver badges15 bronze badges 4
  • 1 What is the problem? Doesn't it show? – LuigiEdlCarno Commented Nov 14, 2013 at 12:34
  • It does show, but I want it to react just like the javascript confirm function. I will explain detailed; lets say I have a function called save(); in this function I am calling the above code var oute = showModalConfirmDialog('Are you sure?'); and depending on the oute I am doing some other stuff, the problem is that the save function is not waiting for the user's response/click, it is return undefined. – Shahe Masoyan Commented Nov 14, 2013 at 12:39
  • I mean, do I have to block something to stop executing other scripts and wait for the user's click? @LuigiEdlCarno – Shahe Masoyan Commented Nov 14, 2013 at 12:42
  • 1 You CANNOT do this. The built-in confirm() is special in that it executes synchronously. All other JS code will execute asynchronously, or block the UI (i.e. the user will not be able to interact). The solution is to either provide the showModalConfirmDialog() with ok()/cancel() callbacks, or have it return a promise (jQuery implementation). – Nikos Paraskevopoulos Commented Nov 14, 2013 at 12:43
Add a ment  | 

1 Answer 1

Reset to default 5

You can't reproduce the behaviour of the native modal. Instead you could use callbacks.

This way :

function showModalConfirmDialog(msg, handler) {
    button1.onclick = function(evt){
        handler(true);
    };
    button2.onclick = function(evt){
        handler(false);
    };
}
showModalConfirmDialog('Are you sure?', function (oute) { 
    alert(oute ? 'yes' : 'no'); 
});

Or this way :

function showModalConfirmDialog(msg, confirmHandler, denyHandler) {
    button1.onclick = confirmHandler;
    button2.onclick = denyHandler;
}
showModalConfirmDialog(
    'Are you sure?', 
    function () { alert('yes'); }, 
    function () { alert('no'); }
);

本文标签: Custom javascript confirm dialog boxStack Overflow