onunload  
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 卸载事件 </title>
<!--用if....else可以实现点击onunload之后关闭页面-->
<script type="text/javascript">   
     window.onunload = onunload_message;   
     function onunload_message(){   
        if(confirm("你确定要离开本页面吗?")){
        window.opener=null;
        window.open('','self');
        window.close();
        }
        else{}
        }
</script>   
</head>
<body>
  <h2>欢迎学习JavaScript。</h2>
  <p>当用户退出页面时(页面关闭、页面刷新等),触发onUnload事件,同时执行被调用的程序。
注意:不同浏览器对onunload事件支持不同。</p>
<input type="button"value="下一页"onclick="onunload_message()">
</body>
</html>在页面中设置一个按钮,用onclick触发