Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop script when using jQuery Dialog?

I'm sure this has been asked here many times. The latest one I saw was on Dec. 2012. But I would like to ask if someone's got a fix/better workaround for this problem.

I'm using jQueryUI Dialog and I would like to ask if it is possible to stop the script execution when the dialog pops up and the user haven't selected an answer yet. Just like the javascript confirm() does.

Here's what I have so far:

jQuery

$("#GlobalDialogBox").dialog({
        modal: true,
        buttons: {
        "Yes": function () { 
            callbackFunctionTrue();
            $(this).dialog('close'); 
         },
        "No": function () { 
            callbackFunctionFalse();
            $(this).dialog('close'); 
         }
      }
    });

   alert("This should fire after answering the dialog box."); //but this fires as soon as the dialog pops up.. :(

The alert is a common part of the script that fires after answering the dialog box regardless of the answer. On my fix, I just inserted the alert in callbackFunctionTrue() and callbackFunctionFlase() and it does the job.

But what if I don't want to do that? How would I stop the script when the dialog pops up so that I can freely add my codes below the dialog script? Does anybody have a better solution? Any help would be appreciated! Thanks in advance! :]

like image 943
Jude Duran Avatar asked Jun 13 '13 02:06

Jude Duran


People also ask

How to disable close button in jQuery dialog?

Now, using the jQuery dialog() method, create the jQuery UI dialog, and in the open event, the handler will write a function to remove the hide button. Select the close button using the class “ui-dialog-titlebar-close” and hide it using the hide() method.

How to remove close button from html?

HTML. Here, we will see how to remove the close button on the jQuery UI dialog using CSS. Approach: By setting the display property of the ui-dialog-titlebar-close class element to none.

What is dialog in jQuery?

A dialog box is a floating window with a title and content area. This window can be moved, resized, and of course, closed using "X" icon by default. jQueryUI provides dialog() method that transforms the HTML code written on the page into HTML code to display a dialog box.


3 Answers

When dialog is being closed it triggers close event. You can do that by adding event listener to your dialog:

$("#GlobalDialogBox").on( "dialogclose", function( event, ui ) {
      alert("something");
});

or in more consistent way:

$("#GlobalDialogBox").dialog({
    modal: true,
    buttons: {
        "Yes": function () { 
            callbackFunctionTrue();
            $(this).dialog('close'); 
         },
        "No": function () { 
            callbackFunctionFalse();
            $(this).dialog('close'); 
         }
    },
    close: function(event, ui){
       alert("something");
    }
});
like image 147
lukaleli Avatar answered Oct 08 '22 17:10

lukaleli


You can't really pause script execution arbitrarily. Callback functions are your best solution in this case, so below your buttons options on the dialog add a close event handler and put your alert and any other code you want to execute after the dialog closes in there.

like image 1
bitwiser Avatar answered Oct 08 '22 15:10

bitwiser


function dialog_confirm(){
  $( "#dialog-confirm" ).dialog({
    //autoOpen: false,
    resizable: false,
    height:140,
    modal: true,
    buttons: {
      "Delete all items": function() {
        $( this ).dialog( "close" );
        dialog_confirm_callback(true);
      },
      Cancel: function() {
        $( this ).dialog( "close" );
        dialog_confirm_callback(false);
      }
    }
  });
}
function dialog_confirm_callback(value) {
  if (value) {
    alert("Confirmed");
  } else {
    alert("Rejected");
  }
}
like image 1
Ramesh Avatar answered Oct 08 '22 16:10

Ramesh