window.addEventListener('beforeunload'function (event) {
var confirmationMessage = '입력 내용이 모두 사라집니다.';
event.returnValue = confirmationMessage;
return confirmationMessage;
});
beforeunload 이벤트는 Mobile Safari에서는 지원하지 않습니다.
<!DOCTYPE html><html><bodyonbeforeunload="return myFunction()"><p>Reload this page, or click on the link below to invoke the onbeforeunload event.</p><ahref="https://www.w3schools.com">Click here to go to w3schools.com</a><script>functionmyFunction() {
return"Write something clever here...";
}
</script></body></html>
<!DOCTYPE html><html><bodyonbeforeunload="return myFunction()"><p>This example demonstrates how to assign an "onbeforeunload" event to a body element.</p><p>Close this window, press F5 or click on the link below to invoke the onbeforeunload event.</p><ahref="https://www.w3schools.com">Click here to go to w3schools.com</a><script>functionmyFunction() {
return"Write something clever here...";
}
</script></body></html>
<!DOCTYPE html><html><body><p>This example uses the HTML DOM to assign an "onbeforeunload" event to the window object.</p><p>Close this window, press F5 or click on the link below to invoke the onbeforeunload event.</p><ahref="https://www.w3schools.com">Click here to go to w3schools.com</a><script>window.onbeforeunload = function(event) {
event.returnValue = "Write something clever here..";
};
</script></body></html>
<!DOCTYPE html><html><body><p>This example uses the addEventListener() method to attach a "beforeunload" event to the window object.</p><p>Close this window, press F5 or click on the link below to invoke the beforeunload event.</p><ahref="https://www.w3schools.com">Click here to go to w3schools.com</a><script>window.addEventListener("beforeunload", function(event) {
event.returnValue = "Write something clever here..";
});
</script></body></html>