Simulate an input() dialog box
The built-in function input() is blocking: the script
execution stops until the user enters a value. It is implemented with the
(blocking) built-in Javascript function prompt() , which opens a
dialog box that can't be customized.
By design, Javascript doesn't support user-defined blocking functions, so
it's impossible to override input() with a function that would
open a dialog box made of HTML elements (an INPUT tag, "Ok" and "Cancel"
buttons) and stop execution until the user clicks on one of the buttons; the
actions to execute when a value has been entered must be defined in a callback
function.
In this example, we use a generic function dialog() that
handles the entry of an integer value in an INPUT tag. It loops until the
value is a valid integer literal.The comments in the source code explain the
interface of this function.
|
|