1 of 20

CSSE280: Events

When you browse the web your browser�Registers different types of events

Checkout the domEvents project from your Git repo

2 of 20

By the end of this unit you should be able to...

  • Describe what happens when an event�occurs
  • Discuss different event types
  • Discuss the different steps of event �handling
  • Bind an event to an element
  • Use parameters with event listeners
  • Use properties of the event object
  • Use methods of the event object

3 of 20

DOM events

The browser’s way of saying “Something just happened

Now your script can respond to these events

  • Scripts do so by updating the content of the page via the DOM

  • This makes the page more interactive

4 of 20

What happens when an event occurs?

5 of 20

Different event types

  • UI Events: occurs when user interacts with browser UI
    • load, unload, error, resize, scroll

  • Keyboard Events: occurs when the user interacts with keyboard
    • Keydown, keyup, keypress

  • Mouse Events:occurs when user interacts with a mouse, trackpad or touchscreen
    • click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout

6 of 20

Different event types

  • Focus Events: occurs when an element gains or loses focus
    • focus, blur

  • Form Events: occurs when a user interacts with form element
    • input, change, submit, reset, cut, copy, paste, select

  • Mutation Events: occurs when DOM structure has been changed by a script
    • DOMSubtreeModified, DOMNodeInserted, DOMNodeRemoved, DOMNodeInsertedIntoDocument, DOMNodeRemovedFromDocument

7 of 20

Terminology

Events fire or are raised

  • When an event has occurred, it is often described as having fired or been raised

Events trigger scripts

  • Events are said to trigger a function or script

8 of 20

Event handling

When a user interacts with the HTML on a page, three steps are involved in running some JavaScript code

  1. Select the element you want the script to respond to (element user interacted with)

  • Bind the event to a DOM node: indicate which event on the selected node(s) will trigger the response

  • State the code you want to run when the event occurs

9 of 20

Binding an event to an element

HTML event handlers: Bad practice! Avoid!!

  • Early versions of HTML introduced a set of attributes that could respond to events on the elements they were added to.
  • The attribute names matched the event names
  • e.g. <a onclick=”hide()”>

Traditional DOM event handlers: Better than HTML event handlers

  • They let you separate HTML and JavaScript
  • Supported in all major browsers
  • Drawback: can only attach a single function to any event
  • element.onevent = functionName;
  • e.g., tileElement.onclick = moveTile;

10 of 20

Binding an event to an element - best approach

DOM Level 2 Event Listeners: Best approach

  • Event listeners were added in an update to the DOM specs in 2000
  • Allow one event to trigger multiple functions
  • Not supported in IE8 and earlier
  • element.addEventListener(‘event’, functionName [, useCapture]);
  • e.g., tileElement.addEventListener(‘click’, moveTile, false);

Can also call element.removeEventListener(‘event’, functionName [, useCapture]);

to remove an event listener

11 of 20

TODO

Complete the exercises in this assignment with your in-class partner.

12 of 20

Using parameters with event listeners

Cannot have parameters after the functionName in an event listener

  • Workaround
    • Wrap the function call in an anonymous function

el.addEventListener(‘blur’,() => {

checkUsername(5);

}, false);

Note: checkUsername(length) needs value for length parameter

The anonymous function runs when the event is raised

13 of 20

Event flow: 3rd argument to addEventListener()

Event flow matters when your code has event handlers

  • on an element AND
  • on one of its ancestor or descendent element

The event always starts at the root (window object), goes down until it hits the target, and then goes back up to the root

The phase where you initiate the event and the event barrels down the DOM from the root is known as the Event Capturing Phase

Up next is Event Bubbling Phase where your event bubbles back up to the root

https://www.kirupa.com/html5/event_capturing_bubbling_javascript.htm

14 of 20

Event flow: matters only in a few cases

<body id="theBody" class="item">

<div id="one_a" class="item">

<div id="two" class="item">

<div id="three_a" class="item">

<button id="buttonOne" class="item">one</button>

</div>

<div id="three_b" class="item">

<button id="buttonTwo" class="item">two</button>

<button id="buttonThree" class="item">three</button>

</div>

</div>

</div>

<div id="one_b" class="item">

</div>

</body>

In which phase do you handle events?

15 of 20

Event flow: every element is informed & notified of event

16 of 20

Event flow

Do you listen/respond to your event as it is goes down in the capture phase?

Do you listen/respond to your event as it climbs back up in the bubbling phase?

element.addEventListener("click", functionName, true);

  • An argument of true means that you want to listen to the event during the capture phase.

  • If you specify false, this means you want to listen for the event during the bubbling phase. (this is the default)

https://www.kirupa.com/html5/event_capturing_bubbling_javascript.htm

17 of 20

The event object: its properties

When an event occurs, the event object tells you information about the event, including the element it happened on

It helps answer the questions:

  • What is the type of the event?
  • Which HTML element is the target of the event?
  • Which key was pressed during the event?
  • Which mouse button was pressed during the event?
  • What was the mouse position during the event?

See http://www.quirksmode.org/js/events_properties.html for more details

18 of 20

Changing default behavior

The event object has methods that change:

  • The default behavior of an element
  • How an element’s ancestors/descendents respond to the event

Some events (e.g., clicking on a link or submitting a form) take the user to another page. w3schools preventDefault()

To prevent the default behavior and keep the user on the same page, use the event object’s preventDefault() method

if(event.preventDefault){

event.preventDefault();

} else { // for IE8

event.returnValue = false:

}

19 of 20

Changing how ancestor/descendent responds to an event

The event object has methods that change:

  • The default behavior of an element
  • How an element’s ancestor/descendent respond to the event

Once you have handled an event using one element, you may want to stop the event from fumbling down to a descendent or bubbling up to its ancestors .

w3schools stopPropagation()

To stop the event from fumbling down or bubbling up, use the event object’s stopPropagation() method

if(event.stopPropagation){

event.stopPropagation();

} else { // for IE8

event.cancelBubble = false:

}

20 of 20

TODO

Consider the example application at

http://javascriptbook.com/code/c06/example.html

Discuss what events fire when the user clicks the microphone or pause button and what code runs in response to the events.

Discuss what events fire when the user types in the textbox and what code runs in response to the events.