1

I am sifting through prompt() and confirm() replacements for JavaScript. I need to create a couple of confirm()s and alert()s that have three or more options. I would like to use the Lightbox class that I am using already. However, none of the replacements I have found can return the result of the operation directly like confirm() and prompt() can:

success = confirm("Success yes / no?");
if (success == true)
 ......

can a replacement be written so that it behaves in the same manner, i.e. that it opens a dialog, waits for user input and returns which buttons have been clicked? Or is this impossible to do (That's what I'm suspecting right now) and you have to work around it by attaching the actions to the "yes / no / whatever" buttons themselves?

Edit: I have decided to unaccept Josh's answer after all. It is totally helpful, but it doesn't answer the question - is there a way to have truly modal dialogs in Javascript? There doesn't seem to be.

Joshua
  • 3,615
  • 1
  • 26
  • 32
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Curious what you mean by "Lightbox class" - is that a reference to a CSS class? Also what do you mean by "waits for user input" - can you clarify that? – Joshua Jan 17 '12 at 16:20
  • @Josh - by "class" I meant "some lightbox product", I'll clarify that. "Wait for user input" means "halts script execution until the user has confirmed one selection or the other". There seems to be no JavaScript way to do that with the exception of synchronous Ajax calls. Your answer is totally helpful and it's *the* answer for most people with this problem, but I'm curious whether there is a way to halt script execution in this way in JS. (It is possible there is not.) – Pekka Jan 17 '12 at 17:07

2 Answers2

3

Most JavaScript frameworks that have UI components have some kind of dialog that will provide callbacks. Check out jQuery UI, for example: http://jqueryui.com/demos/dialog/

I've used it in multiple projects to do exactly what you're describing.

Edit: I was hoping to suggest a workaround. Unfortunately JavaScript does not include methods to override and add additional buttons or options to the default browser-based Alert(), Confirm(), or Prompt() dialogs.

Joshua
  • 3,615
  • 1
  • 26
  • 32
2

Both IE and Firefox 3 have a showModalDialog method which would allow you to display an entire web page modally. However for a truely cross-browser solution you can't use that.

Many of the popular frameworks provide a mechanism to do it by displaying a HTML element and disabling access to the rest of the web page whilst the element is displayed.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306