Problem using ui.dialog as a substitute for confirm()

Problem using ui.dialog as a substitute for confirm()


I'm trying to replace the standard browser confirm() function with a
custom one based on ui.dialog. Here is the function:
function confirm(msg) {
    var response = false;
    // Create the confirm dialog box and open it
    $("#confirm_dialog")
        .html('

'+msg+'

')
        .dialog({
            modal: true,
            title: "Confirmation Required",
            closeOnEscape: false,
            open: function() {
                    $(document).unbind('keydown.dialog-overlay');
                },
            buttons: {
                "OK": function() {
                    $(this).dialog("close");
                    $(this).dialog("destroy");
                    response=true;
                },
                "Cancel": function() {
                    $(this).dialog("close");
                    $(this).dialog("destroy");
                    response=false;
                }
            }
        });
    return response;
}
As you can see, the intent is that if the OK button is clicked, the
function returns true, and if Cancel is clicked, the function returns
false. However, this is not working. The value of response is not
being changed by the ui.dialog buttons callbacks. What am I doing
wrong?