onbeforeunload
이벤트에 할당 된 기존 처리기를 확인하고 해당 핸들러가있는 경우 처리기로 바꾸기 전에 참조를 저장해야합니다.
다음 스크립트 출력을 방출 할 수 귀하의 웹 파트는이 작업을 수행합니다 : 이것은 당신이 페이지에 당신의 자신의 논리를 주입 어떤 코드 때 페이지 언로드를 실행로서 자신의 결정을 할 수 있도록해야
<script type="text/javascript">
// Check for existing handler
var fnOldBeforeUnload = null;
if (typeof(window.onbeforeunload) == "function") {
fnOldBeforeUnload = window.onbeforeunload;
}
// Wire up new handler
window.onbeforeunload = myNewBeforeUnload;
// Handler
function myNewBeforeUnload() {
// Perform some test to determine if you need to prompt user
if (unsavedChanges == true) {
if (window.confirm("You have unsaved changes. Click 'OK' to stay on this page.") == true) {
return false;
}
}
// Call the original onbeforeunload handler
if (fnOldBeforeUnload != null) {
fnOldBeforeUnload();
}
}
</script>
.