Prompting the user for information in Flash using JSFL
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); }
