This article brings you what is onbeforeunload? how to use? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
onbeforeunload is an event that will be triggered when the page is about to be unloaded (updated).
Uninstall (update) is about the unload event, which will be triggered when the page is closed.
window.onbeforeunload = funcRef
funcRef refers to a method, which is a function reference.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>test</title> </head> <body onbeforeunload="return test()"> </body> <script type="text/javascript"> function test(){ return "你确定要离开吗"; } </script> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>test</title> </head> <body> </body> <script type="text/javascript"> window.onbeforeunload=function(){ return "你确定要离开吗"; } </script> </html>
Generally, using window or body directly in a project will cause the refresh and close events of the entire project page to be intercepted.
The general idea of using this interception in a certain page is to mount the event when entering the page, and cancel the mounted event when jumping to the page.
For example in react:
componentDidMount() { window.onbeforeunload = function() { return "真的离开?"; }; } componentWillUnmount(){ window.onbeforeunload = function() { return null; } }
The above is the detailed content of What is onbeforeunload? how to use?. For more information, please follow other related articles on the PHP Chinese website!