-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path26_events.js
160 lines (145 loc) · 5.15 KB
/
26_events.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* ========================================================
* Handling Events
* ========================================================
* Events are actions or occurrences that happen in the browser you are programming for,
* whether caused by users interacting with your webpage or caused by the browser itself.
*/
/**
* ========================================================
* Basic Event Listeners
* ========================================================
* The addEventListener method attaches an event handler function to a specific event on a specific element.
*/
const buttonElement = document.getElementById("myButton");
buttonElement.addEventListener("click", function () {
console.log("Button clicked!");
});
/**
* ========================================================
* The Event Object
* ========================================================
* An Event object is passed as the first parameter to event handlers and contains properties and methods related to the event.
*/
buttonElement.addEventListener("click", function (event) {
console.log("Event object:", event);
});
/**
* ========================================================
* Removing Event Listeners
* ========================================================
* The removeEventListener method detaches an event handler function from an event.
*/
function clickHandler() {
console.log("Button clicked!");
}
buttonElement.addEventListener("click", clickHandler);
buttonElement.removeEventListener("click", clickHandler);
/**
* ========================================================
* Event Propagation: Bubbling and Capturing
* ========================================================
* Event propagation involves three phases: capturing phase, target phase, and bubbling phase.
*/
// Bubbling phase
document.body.addEventListener(
"click",
function () {
console.log("Body clicked! (Bubbling phase)");
},
false
);
// Capturing phase
document.body.addEventListener(
"click",
function () {
console.log("Body clicked! (Capturing phase)");
},
true
);
/**
* ========================================================
* Event Delegation
* ========================================================
* Delegate handling events to a common ancestor rather than individual child elements.
*/
document.body.addEventListener("click", function (event) {
if (event.target.matches(".clickable-item")) {
console.log("Clickable item clicked");
}
});
/**
* ========================================================
* Prevent Default Action
* ========================================================
* The preventDefault method cancels the event's default action if it's cancelable.
*/
buttonElement.addEventListener("click", function (event) {
event.preventDefault();
console.log("Default action prevented");
});
/**
* ========================================================
* Nuances with Examples
* ========================================================
*/
/**
* 1. 'this' Inside Event Handlers
* -------------------------------
* Inside an event handler, 'this' refers to the element to which the event handler is attached.
*/
buttonElement.addEventListener("click", function () {
console.log(this === buttonElement); // true
});
/**
* 2. stopPropagation vs stopImmediatePropagation
* ---------------------------------------------
* stopPropagation prevents further propagation of the event in the capturing and bubbling phases.
* stopImmediatePropagation prevents other listeners of the same event from being called.
*/
buttonElement.addEventListener("click", function (event) {
event.stopPropagation();
console.log("Propagation stopped"); // This will run
});
buttonElement.addEventListener("click", function (event) {
event.stopImmediatePropagation();
console.log("Immediate propagation stopped"); // Won't run if the previous handler is triggered first
});
/**
* 3. Custom Events
* ----------------
* Custom events can be created using the Event constructor and dispatched using dispatchEvent.
*/
const customEvent = new Event("myEvent");
buttonElement.addEventListener("myEvent", function () {
console.log("Custom event fired");
});
buttonElement.dispatchEvent(customEvent);
/**
* 4. Passive Event Listeners
* ---------------------------
* For events like 'touchstart' and 'wheel' that are often related to scrolling,
* you can improve performance by setting the 'passive' option to true.
*/
document.addEventListener("touchstart", function () {}, { passive: true });
/**
* 5. Once Option
* --------------
* The 'once' option ensures that a listener is automatically removed after it's invoked once.
*/
buttonElement.addEventListener(
"click",
function () {
console.log("Button clicked once and listener removed.");
},
{ once: true }
);
/**
* 6. Keyboard Events: keydown, keyup, keypress
* ---------------------------------------------
* Keyboard events include keydown, keyup, and keypress (though keypress is being phased out).
* They provide information about the key pressed.
*/
document.addEventListener("keydown", function (event) {
console.log("Key pressed:", event.key);
});