The duplicate is not really useful indeed. I'm sorry for that.
Based on this answer, this what I would do:
The benefit is that it does not block the whole browser with an infinite loop like in the answer. The option modal
of the jQuery UI dialog simply blocks the current page.
Here's the code:
function confirmDialog(message, onOK, onCancel) {
$('<div>' + message + '</div>').dialog({
modal: true,
buttons : {
"OK" : function() {
$(this).dialog("close");
// if there is a callback, execute it
if (onOK && $.isFunction(onOK)) {
onOK();
}
// destroy the confirmation dialog
$(this).dialog("destroy");
},
"Cancel" : function() {
$(this).dialog("close");
if (onCancel && $.isFunction(onCancel)) {
onCancel();
}
$(this).dialog("destroy");
}
}
});
}
And you can use it this way:
$('button').click(function(e) {
var okHandler = function() {
alert('ok');
};
confirmDialog('Do you really want ?', okHandler);
});
DEMO
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…