最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Overriding javascript confirm with jquery - Stack Overflow

programmeradmin0浏览0评论

I want to override javascript confirm with jQuery dialog box. Here is my overridin code:

window.confirm = function(message, caption = 'Confirmation'){
    $(document.createElement('div')).attr({title: caption, 'class': 'dialog'}).html(message).dialog({
        position:['center',100],
        dialogClass: 'fixed',
        buttons: {
            "OK": function(){
                $(this).dialog('close');
                return true;
            },
            "Cancel": function(){
                $(this).dialog('close');
                return false;
            }
        },
        close: function(){
            $(this).remove();
        },
        draggable: false,
        modal: true,
        resizable: false,
        width: 'auto'
    });
};

And here is my action code:

if(confirm('Are you sure?') == false) { return false; }

This code does not work. How can I do this?

I want to override javascript confirm with jQuery dialog box. Here is my overridin code:

window.confirm = function(message, caption = 'Confirmation'){
    $(document.createElement('div')).attr({title: caption, 'class': 'dialog'}).html(message).dialog({
        position:['center',100],
        dialogClass: 'fixed',
        buttons: {
            "OK": function(){
                $(this).dialog('close');
                return true;
            },
            "Cancel": function(){
                $(this).dialog('close');
                return false;
            }
        },
        close: function(){
            $(this).remove();
        },
        draggable: false,
        modal: true,
        resizable: false,
        width: 'auto'
    });
};

And here is my action code:

if(confirm('Are you sure?') == false) { return false; }

This code does not work. How can I do this?

Share Improve this question edited Jan 18, 2014 at 6:17 Felix 38.1k8 gold badges45 silver badges56 bronze badges asked Jan 18, 2014 at 6:01 user1372430user1372430 3
  • What do you mean by does not work? is it throwing some js error on console ? – dreamweiver Commented Jan 18, 2014 at 6:03
  • For it to work, the function confirm() should return only after the popup is dismissed. Returning true/false from the popup's button event handlers won't cut it. – techfoobar Commented Jan 18, 2014 at 6:04
  • it won't work as expected because the dialog executes asynchronously... in your case the confirm is not returning anything(undefined).. the solution is to use a callback method – Arun P Johny Commented Jan 18, 2014 at 6:04
Add a ment  | 

2 Answers 2

Reset to default 6

It is because the confirm method shows and dialog and it returns before the buttons are pressed.

You can use a callback method to solve it

window.confirm = function (message, callback, caption) {
    caption = caption || 'Confirmation'

    $(document.createElement('div')).attr({
        title: caption,
            'class': 'dialog'
    }).html(message).dialog({
        position: ['center', 100],
        dialogClass: 'fixed',
        buttons: {
            "OK": function () {
                $(this).dialog('close');
                callback()
                return true;
            },
                "Cancel": function () {
                $(this).dialog('close');
                return false;
            }
        },
        close: function () {
            $(this).remove();
        },
        draggable: false,
        modal: true,
        resizable: false,
        width: 'auto'
    });
};

confirm('dd', function () {
    //what every needed to be done on confirmation has to be done here
    console.log('confirmed')
})

Demo: Fiddle

You cannot use it with if..else statement

I know is an old question but for pletness I left my solution, is a plugin for jquery you only need to copy/paste this content save as .js file and include (along with jquery-ui) in your html and all alert and confirm dialog are replaced.

I must point that above solution only calls the callback on user success (press OK button), but I wanted the callback to be called always, this way you can implement more behaviour

jQuery.cambiarAlert = function (options)
{
    var defaults = {
        title: "Atención",
        buttons: {
            "Aceptar": function()
            {
                jQuery(this).dialog("close");
            }
        }
    };

    jQuery.extend(defaults, options);

    delete defaults.autoOpen;


    window.alert = function ()
    {
        var html;

        try {
                html = arguments[0].replace(/\n/, "<br />")
            } catch (exception) {
                html = arguments[0]
        }

        jQuery("<div />", {
                            html: "<div class='.navbar-inverse .navbar-inner'>" + html + "</div>"
        }).dialog(defaults);
    };

    window.confirm = function (message, callback, caption) {
        caption = caption || 'Confirmación'

        $(document.createElement('div')).attr({
            title: caption,
            'class': 'dialog'
        }).html(message).dialog({
            buttons: {
                "Aceptar": function () {
                    $(this).dialog('close');
                    if (callback && typeof(callback) == "function"){
                        callback(true);
                    }
                    return true;
                },
                "Cancelar": function () {
                    $(this).dialog('close');
                    if (callback && typeof(callback) == "function"){
                        callback(false);
                    }
                    return false;
                }
            },
            close: function () {
                $(this).remove();
            },
            draggable: false,
            modal: true,
            resizable: false,
            width: 'auto'
        }).position({
           my: "center",
           at: "center",
           of: window
        });
    };

    return this;
};




$(function ()
{
    $.cambiarAlert();
});

The point here is that I do call callback(true) or callback(false) this way I can write a callback function that use the result as a parameter of the function itself

So in my code I can do:

confirm("Are you sure?", function(result){
    if (result){
       //Perform ajax call
    }
})
发布评论

评论列表(0)

  1. 暂无评论