Click here to Skip to main content
16,018,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to override javascript confirm box with SweetAlert. I have researched also about this but I can't found proper solution.

I am using confirm like this

JavaScript
if (confirm('Do you want to remove this assessment') == true) {
   //something
}
else {
   //something
}

And I am using this for overriding

JavaScript
window.confirm = function (data, title, okAction) {
                swal({
                    title: "", text: data, type: "warning", showCancelButton: true, confirmButtonColor: "#DD6B55", confirmButtonText: "Yes", cancelButtonText: "No", closeOnConfirm: true, closeOnCancel: true
                }, function (isConfirm) {
                    if (isConfirm)
                    {
                        okAction();
                    }
                });
                // return proxied.apply(this, arguments);
            };

Now confirm box is replaced with sweetalert. When user click on Yes button then OK action of confirm box should be called. But this isn't calling

And In above code an error occurred Uncaught TypeError: okAction is not a function.

Please suggest me I should I do for override confirm box.
Posted

1 solution

Strictly speaking, there is no such concept as "override" in JavaScript; even though what you do resembles OOP overriding, but JavaScript is not even close to OOP. Doing such things make little sense. Now, naturally, what happens to the argument depends on how the function is called. If your window.confirm object is called as the original function is supposed to be used, there is only one argument, so onAction simply does not exist. Naturally, you cannot call undefined object, and you cannot call a non-function object.

I would just forget the idea of window.confirm. It is hardly designed for quality refined code. For such purposes, you can better use flexible jQuery dialog:
https://jqueryui.com/dialog[^].

Importantly, with such emulated modal behavior, you do everything on a single Web page, can keep consistent styles, and so on.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900