关闭窗口,这个事件怎么怎么触发不了,ctrl+w
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body >
<p>Close the page to trigger the onunload event.</p>
<script>
window.onunload= function(){
alert('The onunload event was triggered')
}
</script>
</body>
</html>
unload时文档会处于一个特殊状态:
1.所有资源仍存在 (图片, iframe 等.)
2.对于终端用户所有资源均不可见
3.界面交互无效 (window.open, alert, confirm 等.)
4.错误不会停止卸载文档的过程
你可以用beforeunload 但不是所有的浏览器都允许在事件回调中window.alert(), window.confirm(), 和 window.prompt()
在支持的浏览器中可以如下取代alert
但一般业务需要的话event.preventDefault(); 用自己的弹窗
这样并不代表onunload没用 可以做一些标记localStorage等