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 codevar oute = showModalConfirmDialog('Are you sure?');
and depending on theoute
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 theshowModalConfirmDialog()
withok()
/cancel()
callbacks, or have it return a promise (jQuery implementation). – Nikos Paraskevopoulos Commented Nov 14, 2013 at 12:43
1 Answer
Reset to default 5You 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
版权声明:本文标题:Custom javascript confirm dialog box - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744021084a2519809.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论