Chrome, FireFox, Safari에서 코드를 사용해 보았습니다. 아직도 onbeforeunload는 발사하지 않는다. 나도 onunload를 시도했지만 작동하지 않았다.onbeforeunload 이벤트가 실행되지 않는 이유
다음은 실험 할 코드입니다.
<!doctype html>
<html lang="en">
<head>
<title> Output to a Page </title>
<meta charset="utf-8">
<script>
window.onload = init;
window.onclick = clk;
window.onbeforeunload = closing;
function clk() {
window.alert("Ouch !!");
}
function closing() {
console.log("function alrt WORKS !!!!");
window.alert("closing now.....");
}
function init() {
var output = "";
for (var mms = 5; mms > 0; mms--) {
if (mms >= 3) {
output += "Still lots of M&Ms left, so eat more!<br>";
} else {
output += "Getting low on M&Ms, take it easy!<br>";
}
}
output += "All out of M&Ms";
var e = document.getElementById("output");
e.innerHTML = output;
}
</script>
</head>
<body>
<p id="output"></p>
</body>
</html>
현재이 방법만으로 onbeforeunload 이벤트에 대한 전체 크로스 브라우저 지원을 얻을 수 있습니다 (Firefox는 특정 조건에서 실행하지 않습니다). 이 위대한 답변 주셔서 감사합니다! – user3351605