views:

926

answers:

2

Hello,

I need to use a custom prompt, just like the one shown in this jQuery plugin demo. The problem is, all custom prompts depend on a callback, they are asynchronized, i need them to be synchronous.

I need to do something like:

alert("Your name is " + myPrompt("What's your name?"));

Where myPrompt is a modal custom synchronous prompt.

Is it possible?

Thanks, Naimi

+2  A: 

You can use JQuery UI Dialog to display a DIV as a modal dialog.

See http://docs.jquery.com/UI/Dialog/dialog#options

However to support a truely synchronous dialog (a blocking function call) you would need some help from the browser. Not all browsers support it. IE has done for many years and recently mozilla have add showModalDialog to Firefox.

Have you considered using a closure to provide continuation on callback?

Example

function dosomethingwhichneedsprompt()
{
   var a = "stuff";
   var o = {}
   //other code
   displayPrompt("PleaseInputStuff",o)  //o modified by prompt would like this to block
   //continue to use data on o and the variable a after prompt dismissed.
}

Since displayPrompt can't realiable block on all browsers, frameworks will use callbacks instead:-

function doSomethingWhichNeedsPrompt(fnContinue)
{
   var a = "stuff";
   var o = {}
   //other code
   displayPrompt("PleaseInputStuff",o, fnCallback)
   function fnCallback() {
       //continue to use data on o and the variable a after prompt dismissed.
       if (fnContinue) fnContinue();
  }
}

Of course anything calling doSomethingWhichNeedsPrompt would also need to be aware of its async nature and there for doSomethingWhichNeedsPrompt may need to take function parameter which it calls after its done all its work.

AnthonyWJones
A: 

i need them to be synchronous.

You may have to reconsider your needs. With IE7+ disallowing even window.prompt(), there's no cross-browser means of receiving synchronous input.

There is showModalDialog, but it's not globally available and it's also, to my eyes, pretty user-hostile. In most browsers, being synchronous blocks not only your scripts but also the browser's UI, which can be highly inconvenient.

Callbacks aren't so bad IMO:

myPrompt("What's your name?", function(x) {
    alert("Your name is "+x);
});
bobince