onbeforeunload 事件
当窗口即将被关闭时,会触发该事件。此时页面文档依然可见,且该事件的默认动作可以被取消。
事件创建
<body onbeforeunload="fun()"></body>
window.onbeforeunload=function(){fun();};
window.addEventListener("beforeunload", fun);
案例
<!DOCTYPE html>
<html>
<head>
<title>页面刷新或关闭时发送ajax请求</title