onbeforeunload:网页关闭(跳转到别的网页,刷新本页)事件前发生。可以选择继续或者停止动作。
onunload:网页关闭(跳转到别的网页,刷新本页)事件的同时发生,不能停止动作。
<script>
function checkQuit()
{
event.returnValue = "Are you sure to leave?";
}
function alertQuit()
{
alert("Welcome for next time.");
}
</script>
<body onbeforeunload="checkQuit();" onunload="alertQuit();"></body>
IE肯定支持,但是我本机的谷歌和火狐不支持。
本文介绍了网页中onbeforeunload和onunload两个事件的区别及使用方法,前者可在用户离开页面时提供确认提示,后者则在页面关闭时触发且无法取消。通过示例代码展示了如何在HTML中应用这两个事件。

被折叠的 条评论
为什么被折叠?



