0

I am using alertify.confirm() in my spring mvc project.

alertify.confirm(
    "Press OK to Confirm",
    function(){ console.log("ok") },
    function(){ console.log("canceled") });

But whenever i press OK or CANCEL button, it prints "ok" in the console. Why is this happening?

mahfuj asif
  • 1,691
  • 1
  • 11
  • 32

1 Answers1

0

Alertify's confirm has four parameters, not three: title: string, message: string, onOK: function, onCancel: function )

Change your code to this:

alertify.confirm(
    /*title:  */ "Confirm low-effort stackoverflow posting"
    /*message:*/ "Press OK to Confirm posting a question to stackoverflow without doing a quick 15-seconds google search",
    /*ok:     */ function(){ console.log("ok") },
    /*cancel: */ function(){ console.log("canceled, good, the world is better for you having done your research first") }
);

I do see an overload with 3 parameters in their documentation page, [however the source-code of Alertify's confirm.js][2] re-uses the onok parameter for oncancel - I think that's a bug:

main: function (_title, _message, _onok, _oncancel) {
                var title, message, onok, oncancel;
                switch (arguments.length) {
                case 1:
                    message = _title;
                    break;
                case 2:
                    message = _title;
                    onok = _message;
                    break;
                case 3:
                    message = _title;
                    onok = _message;
                    oncancel = _onok;
                    break;
                case 4:
                    title = _title;
                    message = _message;
                    onok = _onok;
                    oncancel = _oncancel;
                    break;
                }
                this.set('title', title);
                this.set('message', message);
                this.set('onok', onok);
                this.set('oncancel', oncancel);
                return this;
            },

Given that AlertifyJS hasn't been significantly updated in six years, I think you should consider using a different dialog library. Consider using native HTML5 <dialog> instead.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • I did check the documentation first. I got error with four parameters. `alertify.min-63a08ef1e1e49d82efcc98b21c1b98ae.js:2 Uncaught Error: fn must be a function at Object.dialog (alertify.min-63a08ef1e1e49d82efcc98b21c1b98ae.js:2)` . Thats why i tried with three parameters. – mahfuj asif Nov 26 '20 at 12:20
  • @mahfujasif The error you posted shows you're using a minified version of `alertify` which may have been altered by the minification process. What happens if you use a non-minified version that you can use with a step-through debugger? – Dai Nov 26 '20 at 12:43
  • @mahfujasif But honestly, I think you should just ditch Alertify.js because it's so old. Why are you using it? – Dai Nov 26 '20 at 12:44