4QL1
4QL1
Fourth Quarter
Lesson 1
Events
What are Events?
It is possible to create dynamic Web pages using JS since it
can detect events. Events are actions that are triggered
either by mouse click, function calls, keyboard strokes,
loading of images, submitting an HTML form, etc.
This is how the onsubmit attribute is usually written. When the submit
button is clicked, it will call a function checkFields(). The function will
check all the fields in the form. If everything is cleared, it will return a
‘true’ so the form will be submitted; otherwise, it will return a ‘false’ so
the form will be cancelled.
Events
• onclick
This is usually used to connect you to another webpage or
to trigger an event call. Example: a button in a webpage.
If that button is pressed, it will trigger a function that will
call a pop-up button.
• onclick
This is usually used to connect you to another webpage or
to trigger an event call. Example: a button in a webpage.
If that button is pressed, it will trigger a function that will
call a pop-up button.
Popup Boxes
• Popup boxes are those windows that pop-out of the
screen.
• They could be before a page is fully loaded, effect of an
event on a page or before leaving a page.
• JavaScript has three kinds of popup boxes: alert box,
user-input prompt box and confirm box.
• These popup boxes can be useful in warning the user,
gathering user input or simply to verify something.
Alert Box
• Usually used when you want to make sure that the
user reads your message such as a warning or a
greeting.
Syntax: alert(“message”);
Try this sample script
<!DOCTYPE html>
<html>
<head><script type=“text/javascript”>
function show_alert()
{alert (“WARNING! WARNING!”);}
</script></head>
<body>
<input type=“button” onclick=“show_alert()”
value=“show alert box”/>
</body>
</html>
User-Input Prompt Box
• This is usually used when you want data to be
prompted first before the user can enter a page. The
user will have to choose either “ok” or “cancel” to
continue. If the user chooses “ok”, the prompt box
will return the encoded value; else, it will return null.
Syntax: prompt(“message”,”defaultvalue”);
Try this sample script When the button is
clicked, a prompt box will
be shown asking for the
user input. The user
<!DOCTYPE html> input will be saved on the
<html> variable name. The user
input will then be
<head><script type=“text/javascript”> validated.
function show_prompt()
{var name=prompt(“Please enter your name:”);
if (name!=null && name!=“”)
{document.write(“Welcome ” +name + ”! Please come in!”);}}
</script>
</head>
<body>
<input type=“button” onclick=“show_prompt()” value=“Click
here to begin”/)
</body>
</html>
Confirm Box
• This is usually used when you want the user to verify
or accept something. The user will have to choose
either “ok” or “cancel”. If “ok” was chosen, the
confirm box will return true; else, it will return false.
Syntax: confirm(“message”);
Try this sample script When the button is clicked, a
<!DOCTYPE html> confirmation box will be shown
asking for confirmation if it will
<html> proceed. The choice will be
saved on the variable ans. The
<head><script type=“text/javascript”> choice will then be used to
function show_confirm() activate the proper block of
code..
{var ans=confirm(“Do you want to proceed?”);
if (ans==true)
{document.write(“You have entered Stage 2”);}
else{document.write(“You will remain at Stage 1”);}}
</script>
</head>
<body>
<input type=“button” onclick=“show_confirm()” value=“Click here
to begin”/)
</body>
</html>
Fourth Quarter e-Portfolio