검색엔진이 사이트를 이해할 때나 앱의 구조를 설계할 때 도움이 된다.
<button onclick="clickButton()">button</button>
window.addEventListener("contentmenu", openContentMenu); // 오른쪽 마우스 클릭 이벤트
window.removeEventListener("contextmenu", openContentMenu); // add 했을때와 같은 함수 넣기
form.onsubmit = handleSubmit;
CustomEvent 생성자를 사용하여 이벤트를 만들면 이벤트의 추가적인 정보도 전달할 수 있다.
const customEvent = new CustomEvent("hello", { message: "hi" });
customButton.addEventListener("hello", function (e) {
console.log(e.message);
});
customButton.dispatchEvent(customEvent); // print "hi"
특정 DOM이 viewport안에 노출이 되는 시점에 callback을 시켜주는 IntersectionObserver API를 사용하여 무한 스크롤을 구현할 수 있다.
const observer = new IntersectionObserver((entries) => {
// entries : 관찰하고 있는 target 목록
entries.forEach(({ isIntersecting }) => {
if (!isIntersecting) {
return;
}
// 다음 새로운 목록 불러오기
})
})
// 목록 아래에 loading 컴포넌트를 배치해놓고 viewport안에 들어오면 다음 목록 불러오는 callback이 실행된다.
observer.observe(loading);
async function getData() {
const response = await fetch(URL); // response.ok 값으로 성공 여부를 알 수 있다.
const json = await response.json();
return json;
}
async function saveData() {
const response = await fetch(URL, {
method: "POST",
headers: {
Authorization: "Bearer token",
},
body: JSON.stringify({
name: "user",
}),
});
const json = await response.json();
}