19

Click here to load reader

JavaScript From Scratch: Events

Embed Size (px)

Citation preview

Page 1: JavaScript From Scratch: Events

JavaScript from Scratch“Events”

1

Page 2: JavaScript From Scratch: Events

Agenda

• Concepts of Event-driven Development

• Applying Events

• Removing Events

• Types of Events

2

Page 3: JavaScript From Scratch: Events

Interactivity

3

Page 4: JavaScript From Scratch: Events

Responsiveness

4

Page 5: JavaScript From Scratch: Events

Enhanced User Experience

5

Page 6: JavaScript From Scratch: Events

The Old Way

onclick=“doSomething()”(HTML)

6

Page 7: JavaScript From Scratch: Events

The Easy Way

var el = document.getElementById(‘foo’);

el.onclick = doSomething;

7

Page 8: JavaScript From Scratch: Events

The Right Way

var el = document.getElementById(‘foo’);

el.addEventListener( ‘click’, doSomething, false);

8

Page 9: JavaScript From Scratch: Events

The IE Way

var el = document.getElementById(‘foo’);

el.attachEvent( ‘onclick’, doSomething);

9

Page 10: JavaScript From Scratch: Events

The Old/Easy Way

var el = document.getElementById(‘foo’);

el.onclick = null;

10

Page 11: JavaScript From Scratch: Events

The Right Way

var el = document.getElementById(‘foo’);

el.removeEventListener( ‘click’, doSomething, false);

11

Page 12: JavaScript From Scratch: Events

The IE Way

var el = document.getElementById(‘foo’);

el.dettachEvent( ‘onclick’, doSomething);

12

Page 13: JavaScript From Scratch: Events

• load

• unload

• resize

• scroll

• focus

• blur

• error

Interface Events

13

Page 14: JavaScript From Scratch: Events

Mouse Events

• mouseover

• mouseout

• mousemove

• mouseup

• mousedown

• click

• dblckick

14

Page 15: JavaScript From Scratch: Events

Form Events

• focus

• blur

• change

• reset

• submit

• keydown

• keyup

• keypress

15

Page 16: JavaScript From Scratch: Events

The Event Reference

http://quirksmode.org/dom/events/index.html

16

Page 17: JavaScript From Scratch: Events

Key Take-aways

• Functions are data types too

• When you pass a function to a listener, you pass a reference to that function

• You can also pass function literals too

17

Page 18: JavaScript From Scratch: Events

Homework

• Create a cross browser event library

• Must support: IE Model and W3 Model

• Should fall back on Netscape Model if nothing else works

18

Page 19: JavaScript From Scratch: Events

Homework

• Required Functions

• addEvent (el, event, callback)

• removeEvent (el, event, callback)

19