[whatwg] Optional non-blocking mode for simple dialogs (alert, confirm, prompt).

Ben Rimmington benrimmington at me.com
Sun Feb 27 02:53:16 PST 2011

> Feature detection needs to be possible without actually calling the function--without actually opening a prompt.  For example, if you're to run the above prompt from a timeout, you need to know that *before* you call prompt(), since you don't want a timeout if the async API is supported.

You'd only need to use window.setTimeout() if window.prompt() ignored your callback argument and didn't return it:

> function promptWrapper(message, default, callback) {
>     var result = window.prompt(message, default, callback);
>     if (callback && (typeof callback === "function")) {
>         if (result !== callback) {
>             /*** INSTEAD OF: callback(result); ***/
>             window.setTimeout(callback, 1, result);
>         }
>         return callback;
>     }
>     return result;
> }

However, as you pointed out, this "feature detection" only occurs after window.prompt() is invoked. A better way might be to test the window.prompt.length property (if 3 or more then non-blocking mode is supported).

> The pattern in new APIs like IDB and FileAPI is to have separate sync and async interfaces, not overloaded behavior, eg:
> promptAsync(message, default, callback);
> Better off just leaving this crusty old API in the legacy bin, though.


More information about the whatwg mailing list