programming/javascript
beforeunload , 사용자가 페이지를 이탈하려고 할 때 방지하기
FaustK
2021. 1. 31. 17:10
사용자가 페이지를 이탈하려고 할 때 컨펌창을 띄우려면 'beforeunload' 를 사용하면 됩니다.
window.addEventListener('beforeunload', (event) => {
// Cancel the event as stated by the standard.
event.preventDefault();
// Chrome requires returnValue to be set.
event.returnValue = '';
});
참고:
developer.mozilla.org/ko/docs/Web/API/Window/beforeunload_event
Window: beforeunload 이벤트 - Web API | MDN
beforeunload 이벤트는 문서와 그 리소스가 언로드 되기 직전에 window에서 발생합니다. 이벤트 발생 시점엔 문서를 아직 볼 수 있으며 이벤트도 취소 가능합니다. 확산 아니오 취소 가능 예 인터페이
developer.mozilla.org