INTERACTING WITH USERS:
JavaScript provides these
built-in top-level functions for interacting with the user:
·
alert(string): Pop-up a box to alert
user for important information. The user will have to click "OK" to
proceed. alert() returns nothing (or undefined).
· prompt(string,
defaultValue):
Pop-up a box to prompt user for input, with an optional defaultValue. prompt() returns the user's input as
a string.
For
example,
var number1 = prompt('Enter the
first integer:');
var number2 = prompt('Enter the
second integer:');
alert('The sum is ' + number1 +
number2); // Concatenate two strings
alert('The sum is ' +
(parseInt(number1) + parseInt(number2))); // Add
two numbers
·
confirm(
string)
: Pop-up a box and ask user to confirm some
information. The user will have to click on "OK" or
"Cancel" to proceed. confirm()
which returns a boolean value.
For example,
var isFriday = confirm("Is it Friday?"); // Returns a boolean
if (isFriday) {
alert("Thank God, it's Friday!");
} else {
alert('Not a Friday');
}
·
document.write(string) and document.writeln(string): Write the specified
string to the current document. writeln() (write-line) writes a
newline after the string, while write() does not.
No comments:
Post a Comment