0
  function Globally() {
    $("#dialogid").dialog({ width: 300, modal: true, show: 'drop', hide: 'drop',
        buttons: {
            "Ok": function () { return true; $(this).dialog('close'); },
            "Cancel": function () { return false; $(this).dialog('close'); }
        }
    });
  }

  function test()
  {
    if(Globally())
      alert("Ok");
    else
      alert("Cancel");
  }

I am trying to make a confirmation dialog and I want it to placed in a function (in this case Globally()) because I am using confirmation dialog in so many different function, but this is not working , the control returns from the Globally() function without getting true or false value. I want it to stop there until user press Ok or Cancel. How can I do this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Gaurav
  • 8,367
  • 14
  • 55
  • 90
  • You can check this fiddle, check the confirm dialog http://jsfiddle.net/mostthingsweb/6Rumh/39/ – Prasenjit Kumar Nag May 19 '12 at 07:08
  • There is a easy to use plugin to do it here: [stackoverflow.com/questions/6457750/form-confirm-before-submit – fd748s7d8](http://stackoverflow.com/questions/6457750/form-confirm-before-submit%20%E2%80%93%20fd748s7d8) –  Sep 27 '12 at 11:30

2 Answers2

1

You'll have to use built in confirm function if you want to run code like that:

var question = confirm("Proceed?")
if (question){
   // continue
 } else {
   // stop
 }

That is because only confirm when used prevent whole javascript execution and allows you to pick one answer or the other (Ok, Cancel).

Dialogs like jQuery dialog can not stop script execution so even if you use

if(Globally())
  alert("Ok");
else
  alert("Cancel");

It'll just execute Globally() function and continue right after it - not waiting for a user answer.

If you really want to use jq dialog then add callback functions to your buttons.

"Ok": function () { callbackFunctionTrue(); },
"Cancel": function () { callbackFunctionFalse(); }

And ditch if/else statement() in test function.

Kane Cohen
  • 1,790
  • 12
  • 17
0

That;s not how it works

Just do

    "Ok": function () { alert('OK'); $(this).dialog('close'); },
    "Cancel": function () { alert('Not ok'); $(this).dialog('close'); }

or

    "Ok": function () { $(this).dialog('close'); test(1) },
    "Cancel": function () {$(this).dialog('close'); test(0) }

with

function test(ok){
  alert(ok?"Ok":"Cancel");
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236