Chapter 6: Events Flashcards

1
Q

What are examples of UI events

A
load
unload
error
resize
scroll
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are examples of keyboard events

A

keydown
key up
keypress

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are examples of mouse events

A
click
dblclick
mousedown
mouseup
mousemove
mouseover
mouseout
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are examples of focus events

A

focus / focusing

blur / focusout

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are examples of form events

A
input
change
submit
reset
cut
copy
paste
select
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are mutation events

A

These occur when the DOM structure of a page changes by a script (to be replaced by mutation observers)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What do we mean by saying an event fired/raised?

A

It happened.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What do we mean by events trigger scripts

A

events find the specified script to run

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the three ways to bind an element?

A

HTML events handlers (nowadays bad practice), dom event handlers and dom level 2 event listeners

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What advantages does event listeners have over DOM event handlers?

A

Allows you to have multiple functions linked to one event.

Event listers are no support in IE8 and under.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What’s the syntax for HTML event handlers?

NB: We don’t write these nowadays

A

these are written as an element attribute and generally preceded by the word on

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you write a DOM event handler

A

you select the element that you want to attach the code to. then attach the event plus the code to run

e. g.
element. onevent = functionName;

switch.onclick = replaceAll;

Notes: when attaching a function we don’t call it, we’re a assigning it. You can only attach one function to an event handler

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do we write an event listener

A

we use the addEvenetListener method to an element. This takes three parameters, the event, a function, a boolean

e. g. element.addEventListener(‘event’, functionName, Boolean)
el. addEventListener(‘blur’, checkName, false);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you pass parameters to functions used for an event?

A

You need a work around and that’s to wrap the function in anonymus function

elUsername.addEventListener(‘blur’, function(){
checkUsername(5);
}, false);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do we support IE5-8 that don’t use addEventListerner

A

They have an alternative method called attachEvent

So we can write an if else

if (elUsername.addEventListerner) {
elUsername.addEventListener('blur', function(){
  checkUsername(5);
}, false);
} else {
elUsername.attachEvent('onblur', function(){
  checkUsername(5);
} );
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is event flow?

A

Elements ar nested inside of other elements. When you click on an element it can also affect elements above it.

If an element has an event handler and one of its ancesytrors or descendants also does, we have a potential clash.

There to kinds
event bubbling - default > specific node to least
event capturing > least specific node topmost specific

17
Q

What is the event object?

A

When an event occurs, the event object tells you information about the event and the element t happened on. Like a global object.

18
Q

What can e stand for?

A

event or error object

19
Q

What are properties and methods of event object?

A

target - most specific element
type - type of event rised
cancelable - Whether you can cancel default behaviour of an element

preventdefault() - stop default behaviour
stopPropagation() - stop event flow from occuring

Note: IE5-8 had quite different names for these properties

20
Q

How do we get the event object in IE5-8?

A

The event object isvavaiable as a child of the window object

We write a function to check in case the event object has been passed to the function

function checkUsername(e) {
  if(!e) {
e = window.event;
}
}
21
Q

How can we improve performance

A

Through event delegation.

Adding new DOM elements
Solves limitations with this keyword
Simplifies code

We can event listeners on parent object, not the individual child objects?

22
Q

How can we change an events default behaviour?

A

preventDefault().

Useful for using js for form validation; stopping links from going to new page

IE5-8 don’t support this but do use returnValue property

if (event.preventDefault) {
  event.preventDefault();
} else {
  event.returnValue = false;
}
23
Q

What does stopPropagation() do?

A

Stop event from bubbling up,and triggering on unwanted elements.

IE5-8 have the cancelBubble property.

if(event.stopPropagation) {
  event.stopPropogation();
} else {
event.cancelBubble = true;
}
24
Q

What’s the best way to determine what element an event triggered on

A

event target property.

An alternative is to use the this keyword (however the event object is preferred method)

25
Q

What different types of events can we respond to

A

DOM Events
HTML5 Events
BOM Events

26
Q

What element should we attach UI events to?

A

window

(some UI events now recommend using other elements e.g. document or body, but this can cause problems across browsers

27
Q

What should you consider when using resize and scroll events?

A

Browsers repeatedly fire the event as it occurs so if you’re running complex code it can slow down the experience

28
Q

How are the focus and blur events coomonly used on forms

A

Show tips and feedback as elements interacted with

Trigger form validation

29
Q

What’s the difference between:
screen
page
client

A

These indicate the position of the cursor in:

screen - device screen
page - where about’s on the page user is
client - viewport/browser window

These use X and Y additions to provide info e.g. screenX, clientY

The positions are all properties of the event object. So you’ld pass the event as a parameter

cursor.value = event.screenY;

30
Q

Does IE8 support input event?

A

No.

Use keydown instead.

31
Q

What are differences between keypress and keydown

A

Keypress - when it ads characters to screen
keydown - any key

If pressed down or held down, causes event to repeatedly fire

32
Q

When does key up event fire

A

After a character appears on screen

33
Q

How can we tell what key was pressed?

A

keyCode property - returns ASCII

Use string method fromCharCode(event.keycode) to show lowercase/key character that it would display

34
Q

When do mutation events happen?

A

When the DOM is changed.

These trigger a mutation event

DOMNodeInserted
DOMNodeRemoved
DOMSubtreeModified
DOMNodeInsertedIntoDocument
DOMNodeRemovedFromDocument
35
Q

What are three popular HTML5 BOM events

A

DOMContentLoaded
hashchange
beforeunload