In previous examples, “Displaying messages in Flash using JSFL” and “Displaying a yes/no dialog box in Flash using JSFL”, we’ve seen how to display simple dialog boxes using the alert() method and yes/no dialog boxes using the confirm() method.
The following example shows how you can prompt the user for a simple value using the prompt() method in JSFL.
Full code after the jump.
// JSFL prompt("What is your name?");
But ideally you’d want to capture the user’s response. The following example shows how you can save the user’s text input into a variable and display the value back using the alert() method:
// JSFL var response = prompt("What is your name?"); alert("You said: " + response);
However, if the user clicked the Cancel button in the prompt dialog, the value of the response variable would be null. The following example shows how you can check for a null response and display an error message:
// JSFL var response = prompt("What is your name?"); if (response == null) { alert("FINE!"); } else { alert("You said: " + response); }
If you wanted to detect whether the user entered anything at all (and didn’t just leave the prompt dialog blank), you could check the length of the response, as seen in the following example:
// JSFL var response = prompt("What is your name?"); if (response == null || response.length == 0) { alert("FINE!"); } else { alert("You said: " + response); }
Finally, the prompt() method accepts an optional second parameter, text, which is the value to pre-populate the user’s response with. The following example shows how you can pass the second parameter to pre-fill out the user’s response:
// JSFL var response = prompt("What is your name?", "Tim"); if (response == null || response.length == 0) { alert("FINE!"); } else { alert("You said: " + response); }
Recent Posts
- Getting started with the TLFTextField class in ActionScript 3.0 and Flash CS5
- Adding tick marks to a Slider control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Creating a custom context menu item in Flash using ActionScript 3.0
- Rotating a Sprite object around its x-axis in Flash using ActionScript 3.0 and Flash Player 10
- Detecting when the user changes the color in a ColorPicker control in Flash using ActionScript 3.0
- Getting the currently selected color as a hexadecimal value on a ColorPicker control in Flash using ActionScript 3.0
- Toggling the text field on the ColorPicker control in Flash using ActionScript 3.0
- Creating a vertical Slider control in Flash using ActionScript 3.0
- Setting the number of columns on a ColorPicker control in Flash using ActionScript 3.0
Categories
- Bitmap (1)
- Components (72)
- Button (19)
- CheckBox (2)
- ColorPicker (6)
- ComboBox (1)
- DataGrid (8)
- FLVPlayback (7)
- Label (9)
- ProgressBar (2)
- Slider (3)
- TextArea (1)
- TextInput (7)
- UILoader (7)
- ContextMenu (1)
- Embed (4)
- ExternalInterface (2)
- Flex (7)
- Font (2)
- General (5)
- Graphics (2)
- JSFL (14)
- Loader (3)
- LoadVars (3)
- Microphone (1)
- migration (12)
- MovieClip (1)
- MovieClipLoader (1)
- Sound (2)
- TextField (1)
- TLFTextField (1)
- TransitionManager (1)
- Tween (1)
- Uncategorized (1)
- URLLoader (4)
- URLVariables (1)
- Video (1)
- XML (2)
Advertising

