How to synchronize jQuery dialog box to act like alert() of Javascript

I am currently working on developing custom dialog box to be used with my application using jQuery. The problem is that the call to create dialog is asynchronous i.e. the line of code after it is executed before it is displayed. Here is what I have done, I have created a function DisplayConfirm() which when called creates a modal dialog. I want to use it like the following:

if(DisplayConfirm()){ //do this else // do that

But I cannot because the line of code written after DisplayConfirm() is executed before the dialog is even created. How can I synchronize this operation so that I don't have to use callback functions?


You cannot write a function in javascript that interacts with the user and blocks the javascript interpreter. confirm can do that because it is a browser built-in, written in C++ (or whatever).


Why do you want to avoid callbacks? They are neat :)

function displayConfirm(confirmStr, okCallback, cancelCallback) { $('<div class=alert />') .append('<p>' + confirmStr + '</p>') .append('<button class=ok-btn>Ok</button>') .append('<button class=cancel-btn>Cancel</button>') .appendTo(document.body) .delegate('.ok-btn', 'click', function (e) { okCallback(e); }) .delegate('.cancel-btn', 'click', function (e) { cancelCallback(e); }); }

There! You see, not too bad :)

Note: I wrote this just from the top of my head. Haven't tested it.

<strong>Edit</strong>: If this isn't clear enough, what I am suggesting here is that you have to use callbacks unless you want to use the native confirm function, just as @PaulPRO stated in a comment to the question.

Once the displayConfirm function is defined as above, you could use it with callbacks like so,

displayConfirm('Are you sure?', function (e) { // do if confirmed }, function (e) { // do if not confirmed });

I wanted to illustrate that it is indeed not too difficult to write a simple callback like functionality and you should be doing this.

Let me know if it still isn't clear.


use alternative option, create one function of your that executed after User generate some event. and call into a dialog box event, that give you to same beheviour.

ex. $("#dialog-modal").dialog({ resizable: false, width: 350, modal: true, buttons: [{ text:"OK", width:80, height:26, click: function() { $(this).dialog("close"); } } ] });


  • How to create a model in DRY pattern with help of flow.js (for async calls) in node.js?
  • Why does it open command prompt when i excute start hurry.bat?
  • Updating ui:defined form content from Master template (JSF & Primefaces)
  • is it possible to a vCPU to use different CPUs from two different hardware computers
  • Python: Execute scp, stdin for password not working
  • Overriding operator*() of iterators
  • Mutable values in an object
  • Is creating an image with Swing thread-safe?
  • regex question for removal of javascript malware
  • Py_InitModule4 with Djapian/Xapian
  • multiplayer game with bluetooth [iphone]
  • Enterprise Architect Synchronize with Code
  • Git and client/server code separation
  • Can't get any output from the taglib
  • Why are `colMeans()` and `rowMeans()` functions faster than using the mean function with `lapply()`?
  • Keep Sql Connection open for iterating many requests? Or close each step?
  • Gem not installing package
  • True privateness in Python
  • Reflection / C# typing errors when publishing an F# class implementing an interface
  • Nginx rewrite equivalent to Apache RewriteRule that converts URL params into QueryString key/value p
  • File random access in J2ME
  • Can't connect Entity Framework to local SQL Server Express
  • How to generate an asynchronous reset verilog always blocks with chisel
  • How to extract a number from a string [duplicate]
  • Is WITH the replacement for a #TEMP table?
  • Refactoring advice: maps to POJOs
  • Python function to read variable length blocks of data from file while open
  • Creating a C++ function that calls other Lua function
  • What is the default HTTP verb in WebApi ? GET or POST?
  • TFS 2015 - Waiting for an agent to be requested
  • Object and struct member access and address offset calculation
  • jQuery ready not fired after rails link_to is clicked
  • HTML download movie download link
  • Updating server-side rendering client-side
  • How to set the response of a form post action to a iframe source?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Turn off referential integrity in Derby? is it possible?
  • Authorize attributes not working in MVC 4
  • Busy indicator not showing up in wpf window [duplicate]
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF