javascript捕获页面窗口关闭事件

javascript捕获窗口关闭事件有两种方法 onbeforeunload() ,onUnload()

用法有两种:

1. function window.onbeforeunload() { alert("关闭窗口")}

function window.onunload() { alert("关闭窗口")}

2. 在body 标签里加入onUnload事件

<body onUnload="myClose()">

然后在javascript里定义myClose()方法

区别:

onUnload方法是在关闭窗口之后执行

onbeforeUnload方法是在关闭窗口之前执行

说明:

两个方法在 页面关闭、刷新、转向新页面 时都触发。

注:只在关闭窗口时触发,而页面刷新的时不触发。

<script language=javascript>

function window.onbeforeunload()

{

if (event.clientX>document.body.clientWidth && event.clientY<0||event.altKey)

{

window.event.returnValue="确定要退出本页吗?";

}

}

</script>