티스토리 뷰
웹 사이트를 체험하는 유저의 동향을 파악하거나 기타 데이터를 쌓기 위해 웹 서비스의 애널리틱스는 거의 반필수적으로 사용한다.
이러한 웹 애널리틱스 툴에선 event bubbling을 통해 event를 탐지하고 데이터를 수집하는 경우가 있는데, stopPropagation을 사용할 경우 event bubbling이 일어나지 않아 해당 툴이 클릭을 캡쳐하지 못할 수 있다.
그러하여 stopPropagation은 완전히 통제된 상황이 아닌이상 사용하지 않아야 한다.
stopPropagation을 대체할 수 있는 방법은 내 경우에는 다음과 같이 구현한다.
const box1Ref = document.getElementById('box-1')
const box2Ref = document.getElementById('box-2')
const box3Ref = document.getElementById('box-3')
box3Ref.addEventListener("click", (e) => {
console.log('box 3 clicked => ', e)
window.alert('box 3 alert!')
})
box2Ref.addEventListener("click", (e) => {
if (e.currentTarget === e.target) {
console.log('box 2 clicked => ', e)
window.alert('box 2 alert!')
}
})
box1Ref.addEventListener("click", (e) => {
if (e.currentTarget === e.target) {
console.log('box 1 clicked => ', e)
window.alert('box 1 alert!')
}
})
'Front-End' 카테고리의 다른 글
Web 3.0 (0) | 2022.01.05 |
---|---|
React 의미론적 컴포넌트화, 최적화에 대해서 (0) | 2021.10.08 |
Infinity scroll, Lazy loading 🥱 | Intersection observer (0) | 2021.08.17 |
[JS] Optional chaining, Nullish coalescing operator 🛒 (0) | 2021.08.04 |
[JS] Live collection, Static collection 📑 (0) | 2021.08.03 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- querySelector
- android-licenses
- flutter doctor
- Global scope
- leftJoin
- jsconfig
- querySelectorAll
- hmr
- tsconfig
- js
- event delegation
- SMUI
- Function scope
- Lexical scope
- getElementsByTagName
- craco-alias
- state_undefined
- CRA
- webpack
- ts(2564)
- url mapping order
- NodeSelector
- start with digit
- DOM API
- getElementById
- code technic
- getElementsByClassName
- Nullish coalescing operator
- undefined
- save-exact
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함