Home>Article>Web Front-end> How to capture window closing event in javascript
How to capture the window closing event with javascript: 1. Use javascript to redefine the [window.onbeforeunload()] event; 2. Add the onUnload event to the body tag.
The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.
Javascript method to capture window closing event:
1. Use javascript to redefine the window.onbeforeunload() event
Just define a function in javascript
function window.onbeforeunload() { alert("Close the window")}
alert() event will be executed before closing the window, you can also The user decides whether to close the window
function window.onbeforeunload() { if (event.clientX>document.body.clientWidth && event.clientY<0 ||event.altKey) window.event.returnValue="确定要退出本页吗?"; }
2. Use the onUnload method
Add the onUnload event to the body tag
body onUnload="myClose()"
Then define myClose( in javascript ) method
But the onUnload method is executed after closing the window, not before closing the window. If you want to make a judgment before closing the window, please use the first method
the above two For the method to be able to successfully close the window, the window must be an independent new window; if it is based on a parent window, it cannot be closed.
导出数据
Sorry ! 您输入的信息在服务器中无法找到
Related free learning recommendations:javascript video tutorial
The above is the detailed content of How to capture window closing event in javascript. For more information, please follow other related articles on the PHP Chinese website!