사용자가 페이지를 이탈하려고 할 때 컨펌창을 띄우려면 '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
'programming > javascript' 카테고리의 다른 글
filter, indexOf, reduce 이용 중복 제거하기 (0) | 2021.01.31 |
---|---|
input 전화번호 입력 받기 (정규표현식) (0) | 2021.01.31 |
nodejs교과서 redis 부분 버전에 따른 에러 (0) | 2019.12.14 |
맥,win10 mysql 실행방법 (0) | 2019.11.26 |
[JS] destructuring 응용 예제 코드 (0) | 2019.11.16 |