window.addEventListener('beforeunload' function (event) {
  var confirmationMessage = '입력 내용이 모두 사라집니다.';
  event.returnValue = confirmationMessage;
 
  return confirmationMessage;
});
beforeunload 이벤트는 Mobile Safari에서는 지원하지 않습니다.
<!DOCTYPE html>
<html>
<body onbeforeunload="return myFunction()">

<p>Reload this page, or click on the link below to invoke the onbeforeunload event.</p>

<a href="https://www.w3schools.com">Click here to go to w3schools.com</a>
    
<script>
function myFunction() {
  return "Write something clever here...";
}
</script>

</body>
</html>
<!DOCTYPE html>
<html>
<body onbeforeunload="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>

<a href="https://www.w3schools.com">Click here to go to w3schools.com</a>
    
<script>
function myFunction() {
  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>

<a href="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>

<a href="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>

'HTML & Script' 카테고리의 다른 글

webview document.referrer 체크  (0) 2019.07.17
autocomplete 속성 작성  (0) 2019.07.05
[html] autocapitalize 속성  (0) 2019.07.03
오프스크린 이미지 지연하기 lazyload  (0) 2019.05.04
iframe 스크롤 히든  (0) 2017.07.04

+ Recent posts