|
|
|
Quality business correspondence.
|
|
|
|
| Basic Javascript alert, prompt and confirm box tutorial
An alert box is a dialog box that pops up with message and OK button. Alert method
display an alert box with an string passed to it. For example alert() will display
an empty dialog box with just OK button. Alert("Hello world") displays a dialog box
with the message "Hello world" and Ok button. It pauses until it’s clicked. You can
display any type of values on an alert box. For example:
Var number=11110999
Alert("The is number: "+number) This simply display The number is 1111099
on an alert box. The + sign is called concatenation sign in javascript because it
ties two values in to one statement.
The following is an example of javascript alert box that pops up when the page is
loaded:
<html>
<body>
<script type="text/javascript">
alert("Hello World!")
</script>
</body>
</html>
| This example display a dialog box with
message Hello World when the page is loaded. |
The following is an example of javascript alert box that pops up when a link is
clicked.
<html>
<body>
<A HREF='javascript:onClick=alert("Not active link!")'>
Click here
</A>
</body>
</html>
| Onclick is an event that understands
the mouse click. We set the Onclick event to the alert box so that when the
active link is clicked, Onclick event executes the alert box
Bellow is the result of this example:
Click |
The following is an example of javascript alert box that pops up when a button is
clicked.
<html>
<body>
<form>
<input type="button" name="click" value="Click To alert" onclick='alert("You
clicked a button to display an alert box")'>
</form>
</body>
</html>
| Again on Click event is used to display
the alert box when the button is clicked.
Bellow is the result of this example:
|
Prompt Box
The prompt() is a method of the window object, just like the alert() box with extra options.
The format for prompt() is similar to alert() except for one addition. Prompt has
text field to type a value. It also has OK and CANCEL buttons while alert has only
one button. Here is syntax example of prompt box:
prompt("Type your name:"). This prompt method
display type your name: message on a prompt box and text field with the
default text of undefined. We can display default text in the text field
by simply doing the following:
prompt("Type your name:","Type here") The information submitted to the
prompt() can be stored in a variable, example:
var name=prompt("Type your name:","Type here") Once we have a value
from prompt box stored in a variable. We can display it on a message box, write it on
the current browser or display it on a new window. The following is an example of
prompt box that displays the provided value on a message box:
<html>
<body>
<script type="text/javascript">
var name = prompt("What is your name:", "Type your name here");
alert("Your name is: "+name)
</script>
</body>
</html>
| This example
display a prompt box when the page is loaded with message Type your name
and default text field value of Type your name here. Once the a name
is provided, it stores the variable name and display the name on an
alert box. |
Confirm Box
The JavaScript confirm box differs from a regular alert box in that it
provides two choices for the user, OK and Cancel to confirm the request.
You can use a variable and
if statement to determine if the Ok or Cancel button is clicked and work on your
way to give a proper respond. The following is a syntax example to display
confirm box: confirm ("Do you want to continue?"). This a dialog box with the message Do you want to continue and Ok and
Cancel buttons.
The following example tells you which button you have clicked:
<html>
<body>
<script type="text/javascript">
var answer = confirm ("Do you want continue?")
if (answer)
alert ("You say ok!")
else
alert ("You say cancel!")
</script>
</body>
</html>
|
We stored the confirm objects in a variable
answer then used if statement
to determine which button clicked.
The default and expected button is Ok button so
we say if answer is equal to ok then go to specify location. Else
will be true if not the default value is current value of the variable.
| |
The following is another example that directs the user to another page
when Ok button is clicked:
<html>
<body>
<script type="text/javascript">
var answer = confirm ("Do you want different page?")
if (answer)
window.location="http://www.insurancehotline.com"
else
alert ("Nothing happened!")
</script>
</body>
</html>
|
We stored the confirm objects in a variable
answer then used if statement
to determine which button is click.
The default and expected button is Ok button so
we say if answer is equal to ok then display the message You say ok. Else
will be true if not the default value is current value of the variable answer.
|
Form Validation
String, Date, Array objects
|
|
|