javascript捕获窗口关闭事件有两种方法 onbeforeunload() ,onUnload()javascript
用法有两种:html
1. function window.onbeforeunload() { alert("关闭窗口")}java
function window.onunload() { alert("关闭窗口")}htm
2. 在body 标签里加入onUnload事件blog
<body onUnload="myClose()">事件
而后在javascript里定义myClose()方法ip
区别:io
onUnload方法是在关闭窗口以后执行event
onbeforeUnload方法是在关闭窗口以前执行function
说明:
两个方法在 页面关闭、刷新、转向新页面 时都触发。
注:只在关闭窗口时触发,而页面刷新的时不触发。
<script language=javascript>
function window.onbeforeunload()
{
if (event.clientX>document.body.clientWidth && event.clientY<0||event.altKey)
{
window.event.returnValue="肯定要退出本页吗?";
}
}
</script>
摘自:http://www.cnblogs.com/sunrack/articles/1355742.html