0

I am using a JQ dialog to replace the native js confirm() function. To use it as a central function I've put it into a function.

var dialogID = "dialog_" + createUUID();
var width = 300;
var height = 150;
var url = "";
var formObj = "";
function jqConfirm(dialogID,title,txt,width,height,url,formObj) {
    var dialog = $("#" + dialogID);
    // generate the HTML
    if (!$("#" + dialogID).length) {
        dialog = $('<div id="' + dialogID + '" style="display:hidden;" class="loading"></div>').appendTo('body');
}
dialog.dialog({
    bgiframe: true,
    autoOpen: false,
    width: width,
    height: height,
    minWidth:100,
    minHeight:100,
    maxWidth:980,
    maxHeight:700,
    modal: true,
    dialogClass: 'dialogWithDropShadow',
    resizable:false,
    draggable:false,
    show:'fade',
    hide:'fade',
    title: title,
    buttons: [
        {
            text: "OK",
            "class": 'mscbutton',
            click: function() {
                if (formObj.length) {
                $(formObj).submit();
            } else if (url.length) {
                document.location.href = url;
            }
        $(this).dialog('close');
            }
        },
        {
            text: "Cancel",
            "class": 'mscbutton',
            click: function() {
                $(this).dialog('close');
            return false;
            }
        }
    ],
});
// fill the dialog
$(dialog).html(txt);
dialog.dialog('open');
$(dialog).removeClass('loading');
}

I call the function from within other JS files. A js confirm() I'd use like this:

if (confirm('confirm me')) doThis();

But this way won't work with my function because all I get is an "undefined".

if (jqConfirm(paramstring...)) doThis();

The dialog opens and works just fine but seems to return nothing. I know I am doing something wrong. But what?

Thank u all Greetings

Bernhard Kraus
  • 329
  • 1
  • 3
  • 21
  • possible duplicate of [Is a Modal Confirm Box Using JQuery Possible?](http://stackoverflow.com/questions/878710/is-a-modal-confirm-box-using-jquery-possible) – JJJ Aug 02 '13 at 09:46

1 Answers1

1

You can't do it that way. Only the built-ins (alert, confirm, etc.) can actually halt execution of JavaScript on the page waiting for the user to do something.

Instead, you have to use a callback, e.g.:

jqConfirm(paramstring, function(result) {
    if (result) {
        doThis();
    }
});

You'd trigger the callback passed into jqConfirm by using the callbacks you have on the buttons:

// Add `callback` to this somewhere appropriate; I've just stuck it at the
// end. Consider using an options object rather than lots of individual
// parameters.
function jqConfirm(dialogID, title, txt, width, height, url, formObj, callback) {
    // ...
    dialog.dialog({
        // ...
        buttons: [
           {
               text: "OK",
               "class": 'mscbutton',
               click: function () {
                   if (formObj.length) {
                       $(formObj).submit();
                   }
                   else if (url.length) {
                       document.location.href = url;
                   }
                   $(this).dialog('close');
                   callback(true);            // <== Do the callback
               }
           },
           {
               text: "Cancel",
               "class": 'mscbutton',
               click: function () {
                   $(this).dialog('close');
                   callback(false);           // <== Do the callback
               }
           }
        ],
    });
    // ...
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875