Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object...

24
Lesson 2 Event Handling
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    220
  • download

    1

Transcript of Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object...

Page 1: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

Lesson 2

Event Handling

Page 2: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

Object Event Handlers

• Most of the objects that make up the Document Object Model respond to asynchronous, user generated events through predefined event handlers that handle the event and transfer control to a user provided event handling function

• Each object has particular events that they will respond to

• the way you specify an event handler is by adding an additional attribute to the HTML tag that specifies the event and the particular handler

• <input name=bt1 type=button value=ok onClick=“acb( );”>– if the button is click the function abc( ) will be run

Page 3: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

Alert

The “Alert” function is useful in user notification and debugging of Javascripts.

Pops up a message in a pop-up dialog box

Ex, alert(“help, help, help”);

Page 4: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

Events

• onAbort• onBlur• onChange• onClick• onError• onFocus• onLoad• onMouseOut• onMouseOver• onReset• onSelect• onSubmit• onUnload

Page 5: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

onAbort

• Activated when a user aborts the loading of an image

<img name=ball src=images/ball.gif onAbort=“alert(‘You missed a nice picture’)”>

Page 6: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

onBlur

• Used with frame, select, text, textarea and window objects

• invoked when an object loses focus

• use with select, text and textarea for data validation

Page 7: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

onChange

• Used with select, text and textarea objects

• use instead of onBlur to validate only if a value has changed

<form>

Color: <select onChange=“processSelection()”>

<option value=“R”>Red

<option value=“G”>Green

<option value=“B”>Blue

</select>

</form>

Page 8: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

onClick

• Used with button, checkbox,link, radio, reset, and submit objects.

<input type=button name=btn1 value=“Click Me” onClick=“alert(‘button was clicked’;” >

Page 9: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

onError

• Used with image and window objects to invoke a handler if an error occurs while an image or window is loading.

• Setting window.onerror = null will prevent users from seeing JavaScript generated errors

Page 10: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

onFocus

• Used with frame, select, text, textarea and window objects.

• Just the opposite of onBlur; i.e. invoked when the object gets focus.

<body bgcolor=“lightgrey” onBlur=“document.bgColor=‘black’ onFocus=“document.bgColor=‘white’” >

Page 11: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

onLoad

• Used with window, frame and image objects (use with <body ….><frameset ….> and <img ...>)

• Activated when the body, frameset, or image is loaded

<img name=spinball src=images/spinball.gif onLoad=“startAnimation(this)”>

Page 12: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

onMouseOut and onMouseOver

• Used with area and link objects

• user moves mouse off of an area or link

<map name=flower>

<area name=top coords=“0,0,200,300 href=“javascript:displayMessage()”

onMouseOver=“self.status=‘when you see this message click your left mouse button’ ;

return true”

onMouseOut=“self.status = ‘’ ; return true”>

Page 13: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

onReset

• Used with form objects

<form onReset=“alert(‘the form has been reset’)” >

Page 14: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

onSelect

• Used with text and textarea objects

• run some Javascript whenever a user selects a piece of text in a text or textarea object

<input type=text name=line onSelect=“showHelp()” >

Page 15: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

onSubmit

• Use with form objects to run a handler whenever a form has been submitted.

• Usefull to validate all fields prior to actual submission

Page 16: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

onUnload

• Just like onLoad but the handler is run when the window/frame is exited

<body onUnload=“cleanup()” >

Page 17: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

Advanced Event Handling

• Previous events are event objects caused as a result of interaction with forms tags

• There is another Event object made necessary by the advent of Dynamic HTML– allows a more powerful way of trapping and

processing events

Page 18: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

The Event Object• Provides a set of properties (no methods) that event

handling routines use as constants– property group 1 (modifier keys)

• Alt– Event.ALT_MASK

• Ctrl– Event.CONTROL_MASK

• Shift– Event.SHIFT_MASK

• Meta (Windows key, or Command key on Mac)– Event.META_MASK

– property group 2 (other event types)

• one property for each event type in DOM– ex. Event.CLICK //generic click event

Page 19: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

Capturing Events

• Remember the hierarchy:

window

link anchor layer form applet image area

history document location toolbar

text radio button fileUpload select

textarea

password

checkbox reset

submit

option

Page 20: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

Capturing Events (cont)• Window, document and layers all have a

captureEvents( ) method

• the methods require one or more parameters that will tell it which events to capture; do this as the page loads (head section is good)– window.captureEvents(Event.KEYPRESS)– window.captureEvents(Event.KEYPRESS) |

Event.CLICK)

• assign a function to handle the captured event– window.onClick = processClick

Page 21: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

Turning event capture off

• To turn event capture off use the releaseEvents( ) method

• use the same parameters as captureEvents( )

Page 22: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

Event Forwarding

• Use your generalized event handler (for all clicks) to do high level processing and then allow the individual handlers do their job for the specific events by using routeEvent(e) to pass the event through the hierarchy to the target handler.

• This let you minimize the amount of code you would repeat in writing the handlers

• this lets your events to travel through the hierarchy automatically

• this works different for Netscape and Internet Explorer– in Netscape events travel down the hierarchy to the target; in IE they

travel up the hierarchy (from the object to the window)

– in Netscape events are only generated by forms objects and a few others; in IE almost any tag can generate an event

Page 23: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

handleEvent

• The handleEvent(e) method allows you to route and event to anywhere in the hierarcy, regardless of the hierarchy.

• the object reference is the reference of the object to handle the event; passing the event object as a parameter, like routeEvent and captureEvent

• as long as the target object as an event handler it will process it just as if it had received it directly from the system

Page 24: Lesson 2 Event Handling. Object Event Handlers Most of the objects that make up the Document Object Model respond to asynchronous, user generated events.

Problems

• The event model is not standardized across all browsers, just as dynamic HTML is far from standardized.

• Forewarned is forearmed, this is a place where you could really get into trouble because of browser incompatibilities.