Java Script prompt Message Box
By Stephen Bucaro
In Java Script programming, there are many occasions when you need to display
a short message to the user, or request a bit of information from the user.
In these instances, you can use a message box.
Java Script provides three types of message boxes, the alert, confirm, and
prompt. They are all easy to create and use.
• These message boxes are "modal" dialog boxes, meaning that
program flow halts while the message box is visible. The user is unable to use
the page that spawned the message box until they click on a button to close the
message box. This may be disruptive to the user. You can also design your own
custom message box, which you may or may not choose to make modal.
A prompt presents a message, a text box for user input, an [OK] button
and a [Cancel] button. If the user enters a response in the text box and clicks on
the [OK] button, the prompt returns the users response. If the user clicks on
the [cancel] button, whether the user enters a response in the text box or not,
the prompt returns null. Clicking on either button dismisses the dialog
box. Example code to create a prompt message box is shown below.
var response = prompt("Message")
if(response != null)
You can supply a default response. If the default response is acceptable
to the user, they can just click on the [OK] button.
If you don't supply a default response, the text "undefined" will appear in
the input text box and, if the user clicks on the [OK] button without entering
a response in the text box, the prompt will return undefined.
If you don't have a specific default response, but you don't want "undefined"
to appear in the text box, provide an empty string as the default response, as
var response = prompt("message","")
if(response != null && response != "")
• If you're prompting the user for a numerical input, don't
forget to use the isNaN function along with parseInt or parseFloat
to convert the string input value to a number.
More Java Script Code:
• Java Script Arithmetic Operators
• Java Script Math.cos Method
• Extract Part of a String
• The if/else Structure
• Java Script Math.tan Method
• Remove Blank Spaces From Ends of a String
• Generating Random Numbers
• Java Script Character Encoding and Decoding
• Find a Character or a Substring Within a String