CPIT-405 · WEB APPLICATIONS
Events & Cookies
Part 2: Pages that react, and remember
Mouse eventsInline handlersaddEventListenerKeyboard eventsremoveEventListenerCookiesTheme example
01 · MOUSE EVENTS
User acts → event fires → your code runs
1 · the user
Click me
→
2 · the browser
"click"
→
function handleClick() { // 3 · your code}
clickpress and release
dblclicktwo quick clicks
mousedownbutton goes down
mouseupbutton is released
02 · INLINE IN HTML
Way 1: onclick in the HTML
<button onclick="handleClick()">Click me</button> <script>function handleClick() { alert("The button was clicked");}</script>
With parentheses: the attribute is code that calls the function
Click me
The button was clicked
✓ Quick to write
✗ Mixes HTML with JavaScript
03 · IN JAVASCRIPT
Way 2: addEventListener
let button = document.getElementsByTagName("button")[0];button.addEventListener("click", handleClick); function handleClick() { alert("The button was clicked");}
handleClick

You hand over the function. The browser calls it on every click.

handleClick()

Calls it right now, once, and passes its result instead.

04 · KEYBOARD EVENTS
keydown and keyup
A
down: keydownup: keyup
// event log
keydown event.key = "A"
keyup event.key = "A"
keydown event.key = "Enter"
keyup event.key = "Enter"
<input onkeyup="handleKey(event)">
input.addEventListener("keyup", handleKey);
05 · REMOVING A LISTENER
Make it work only once
button.addEventListener("click", handleClick); function handleClick() { alert("Clicked! The listener will be removed"); button.removeEventListener("click", handleClick);}
Click me
Click 1 ✓ runs
Click 2 nothing
listener: attachedlistener: removed
Same event name and the same function, or nothing is removed
06 · IN REAL PAGES
keyup → filter
TimeTeams
3:00Cavaliers @ Pistons
3:30Celtics @ Nets
4:00Hawks @ Bulls
click → sort
TeamWins ▼
Celtics48
Heat40
76ers52
Raptors46
Bucks56
Bucks56
76ers52
Celtics48
Raptors46
Heat40
07 · COOKIES
Small data the browser keeps for you
theme=darklang=arusername=Fatima
one cookie jar per origin
https://protocolwww.example.comdomain:8080port/login
Not encrypted: never store passwords or sensitive data
08 · SETTING COOKIES
document.cookie = "username=Fatima";document.cookie = "background=dark";document.cookie = "lang=ar; max-age=" + 2 * 24 * 60 * 60;
max-age=172800seconds (2 days)
expires=…UTCan exact date
No max-age or expires: a session cookie, deleted when the browser closes
cookie jar
username=Fatima background=dark lang=ar 2 days
each line adds, nothing is overwritten
09 · READING COOKIES
One string, split twice
document.cookie → "background=dark; lang=ar; profileId=1dsf32f39"
.split(";")
"background=dark"
" lang=ar"
" profileId=1dsf32f39"
.split("=")[1]
dark
ar
1dsf32f39
10 · DELETING COOKIES
No delete(). Expire it instead.
document.cookie = "background=dark; expires=" + new Date(2000, 0, 1).toUTCString() + "; path=/";
JAN12000
a date in the past →
lang=ar background=dark username=Fatima
11 · PUTTING IT TOGETHER
A theme switch that remembers
Light ▾
Dark ▾
1click picks Dark
2saved: theme=dark
3come back later: still Dark
NEXT
Your turn
Open the practice platform and start the Part 2 tab
Event labPredict the behaviourEvent challengesCookie challenges
0:00 / 0:00
← All partsPractice Part 2 →
Space: play / pause · ← →: scenes · M: voice on / off · C: subtitles · F: fullscreenDeveloped by: Fatmah Bamashmoos