Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine...
-
Upload
philip-harrington -
Category
Documents
-
view
222 -
download
1
Transcript of Event Handlers CS101 Introduction to Computing. Learning Goals Learn about event handlers Determine...
Learning Goals
• Learn about event handlers
• Determine how events are useful in JavaScript
• Discover where events can be handled on a page
• Find out what the events are in JavaScript
• Determine the event handlers that are used for each event
• Be able to code scripts that make use of event handlers
What is an Event?
• Something that happens when the User of the page performs some sort of action
• Possible actions– Mouse click– Mouse over
• An action triggers an event
What Is an Event Handler?
• Predefined JavaScript keyword
• Handles an event on a Web page– Identifies an event and then performs a
specific task or set of tasks
Why Event Handlers Are Useful
• Enables Web page to react to an action by the User thus making scripts that are interactive and fun to use
• Example– Sending an alert to the User when he/she
moves mouse over a link
Using Event Handlers
• Need to know keywords for event handlers and where to place event handler in script
• OnClick event handler– Used to make something happen when the
User clicks something on the document
• Add the onClick event handler as an attribute to a form button
• Difference between and event handler attribute and HTML attribute– We can add JavaScript code inside an event handler
attribute rather than just an attribute value
• Notice the quote marks– Double for code– Single for message (to avoid confusion with code)
• We can perform multiple actions on the script event
• We can code in two alerts and execute multiple commands
• Keep on one line between event handler keyword and semicolon– Line break could cause an error
<html><body><form><input type="button" onClick="window.alert('Hi');window.alert('Bye');"></form></body></html>
<html><body><form><input type="button" onClick="window.alert('Hi');window.alert('Bye');"></form></body></html>
• If code is long, use a function
• Define function in head
• Call function from body
• This shortens code within event handler
• Can also reuse function elsewhere in a button or script
<html><head><title>Events & Functions</title><script language="JavaScript"><!--function hi_and_bye(){
window.alert('Hi');window.alert('Bye');
}//--></script></head><body><form><input type="button" onClick="hi_and_bye();"></form></body></html>
<html><head><title>Events & Functions</title><script language="JavaScript"><!--function hi_and_bye(){
window.alert('Hi');window.alert('Bye');
}//--></script></head><body><form><input type="button" onClick="hi_and_bye();"></form></body></html>
Event Event Handler Event Trigger
Click onClick User clicks an area
Mouseover onMouseOver User moves the mouse over link
Mouseout onMouseOut User moves the mouse away from link
Load onLoad Web page finishes loading
Unload onUnload User leaves current page
Focus onFocus User gives focus to something
Blur onBlur User removes focus from something
Change onChange User changes contents of form element
Submit onSubmit User submits form on page
<html><head><title>onClick Event Handler</title></head><body><form><input type="button" value="Do not click here"
onClick="window.alert('I told you not to click me!');"></form></body></html>
<html><head><title>onClick Event Handler</title></head><body><a href="#" onClick="window.alert('Hey! You clicked me!');">Don't click me</a></body></html>
The Mouseover Event (onMouseOver)
• User moves the mouse over an area
• Used in– Text link– Linked image
<html><head><title>onMouseOver Event Handler</title></head><body><a href="#" onMouseOver="window.alert('I told you not to try and click me!);">Don't try clicking me!</a></body></html>
<html><head><title>onMouseOut Event Handler</title></head><body><a href="#" onMouseOut="window.alert('You did not click me!);">Click me!</a></body></html>
<html><head><title>onLoad Event Handler</title></head><body onLoad="window.alert('The page has finished loading.');">Body text.</body></html>
<html><head><title>onUnload Event Handler</title></head><body onUnload="window.alert('Come back soon.');">Body text.</body></html>
<html><head><title>onFocus Event Handler</title></head><body><form>Enter your name: <input onFocus="window.alert('Capitalize your name.');"></form></body></html>
Problem 11
• User gives focus to a first text box
• User removes focus from first text box
• User gives focus to second text box
<html><head><title>onBlur Event Handler</title></head><body><form>Give this text box focus: <input type="text" onBlur="window.alert('Hey! Come back!');"><br>Then give this box focus to blur the first one:<input type="text"></form></body></html>
What We Learnt Today ...
• Learnt about event handlers
• Determined how events are useful in JavaScript
• Discovered where events can be handled on a page
• Found out what the events are in JavaScript
• Determined the event handlers that are used for each event
• Coded scripts that made use of event handlers