/**
* @name fnBackWarning
* @description 뒤로가기 시 변경된 값이 있으면 경고창 표시 기능
*/
function fnBackWarning() {
// 브라우저 세션 히스토리와 상태 변경이 있으면
if (window.history && history.pushState) {
// window load 시
addEventListener('load', function () {
// 가짜 상태변경을 한다
history.pushState(null, null, null);
// 히스토리 엔트리가 변경될 시
addEventListener('popstate', function () {
// document에 textarea태그를 가진 모든 요소를 textarea변수에 담는다.
var textarea = document.getElementsByTagName('textarea'),
// document에 input태그를 가진 모든 요소를 input변수에 담는다.
input = document.getElementsByTagName('input'),
inputCheck = false;
// textarea 갯수만큼 반복
for (var i = 0; i < textarea.length; i++) {
// 현재 textarea요소의 data-edited속성값이 있으면
if (textarea[i].getAttribute('data-edited')) {
// inputCheck를 true로한다.
inputCheck = true;
}
}
// input 갯수만큼 반복
for (var i = 0; i < input.length; i++) {
// 현재 input요소의 data-edited속성값이 있으면
if (input[i].getAttribute('data-edited')) {
// inputCheck를 true로한다.
inputCheck = true;
}
}
// inputCheck가 true면
if (inputCheck) {
// 경고 메세지를 표시하고
var stayOnPage = confirm("변경사항을 저장 하시겠습니까?");
// 취소를 누르면
if (!stayOnPage) {
// 뒤로간다.
history.back()
}
// 확인을 누르면
else {
// 가짜 상태변경을 한다
history.pushState(null, null, null);
}
}
// inputCheck가 false면
else {
// 뒤로간다.
history.back()
}
});
});
}
}
fnBackWarning();