1 mei 2016
Javascript Events / keycodes
En eventueel een subtitel om de nieuwsgierigheid aan te wakkeren
Javascript move left
2
1 mei 2016
Javascript move left
3
Javascript move left setTimeout()
4
1 mei 2016
Javascript move left setTimeout()
5
Javascript move Mouse event over
6
1 mei 2016
Javascript handler
7
Event handler
Javascript handler
8
Event handler
By convention the event is represented by the variable 'event()'.
In some browsers the event must be explicitly passed to the function, so as a precaution it's often
best to include a conditional to test that the event() variable has been passed, and if it hasn't then to
use an alternative method that works on those other browsers:
1 mei 2016
Javascript handler
9
Event handler
By convention the event is represented by the variable 'event()'.
In some browsers the event must be explicitly passed to the function, so as a precaution it's often
best to include a conditional to test that the event() variable has been passed, and if it hasn't then to
use an alternative method that works on those other browsers:
Javascript handler
10
Event attributes
1 mei 2016
Javascript handler
11
Mouse/keyboard
attributes
Javascript handler
12
Standard Event
Attributes
1 mei 2016
Javascript handler
13
Event Handlers as HTML attributes
Javascript handler
14
Event Handlers as HTML attributes
1 mei 2016
Javascript handler
15
Writing an event handling script
element.onclick = doSomething;
if (element.captureEvents)
element.captureEvents(Event.CLICK);
function doSomething(e) {
if (!e) var e = window.event // e refers to the event
}
Javascript handler
16
Writing an event handling script
The target/srcElement properties contain a reference to the HTML element the event originally took place on.
Very useful, but when the event is captured or bubbles up the target/srcElement doesn’t change: it’s still the element the event originally took place on. (See the Event properties page for
target/srcElement, see the this page for the this keyword)
1 mei 2016
Javascript handler
17
Reading out properties
Javascript handler
18
Event order
1 mei 2016
Javascript handler
19
Keycode example
document.addEventListener("keydown", function(event) {
console.log(event.which);
}
Javascript handler
20
Keycode example
1 mei 2016
Javascript handler
21
Keycode example
Javascript handler
22
Keycode
example
1 mei 2016
Javascript handler
23
Keycode example
windows.onload
Javascript handler