Home > Article > Web Front-end > How to use js to determine whether the page is refreshed or closed (onbeforeunload and onunload events)_javascript skills
Onunload and onbeforeunload are called when refreshing or closing. They can be specified through window.onunload in the <script> script or specified in <body>. The difference is that onbeforeunload is executed before onunload, and it can also prevent the execution of onunload. <br>Onbeforeunload is also called when the page is refreshed or closed. Onbeforeunload is called when going to the server to read a new page, but it has not yet started reading; while onunload has read the new page that needs to be loaded from the server. Called when the current page is about to be replaced. Onunload cannot prevent the page from being updated and closed. And Onbeforeunload can do it. <br><br>Onload is executed only when the page is loaded. <br>Onbeforeunload is executed first when the page is closed, and onunload is finally executed. <br>Onbeforeunload is executed first, then onunload, and finally onload is executed when the page is refreshed. <br><br><strong>1. onbeforeunload event: </strong> <br> Description: Firefox and IE currently support the onbeforeunload event in the three major mainstream browsers, but Opera does not yet support it. <br> Usage: <br> ·Object.onbeforeunload = handler <br> ·<element onbeforeunload = “handler” … ></element> <br> Description: <br> When the event is triggered, a message pops up with confirmation and cancel the dialog box. If you confirm, you will leave the page. If you cancel, you will continue to stay on this page. The handler can set a return value as the displayed text of the dialog box. <br><br> Triggered by: <br> ·Close the browser window <br> ·When going to other pages through the address bar or favorites <br> ·When you click one of the return, forward, refresh, and homepages <br> ·When clicking a url link that goes to other pages<br>·When calling any of the following events: click, document write, document open, document close, window close, window navigate, window NavigateAndFind, location replace, location reload, form submit. <br> ·When using window open to open a page and pass the name of the window on this page to the page to be opened. <br> ·When reassigning the value of location.href. <br> ·When submitting a form with a specified action through the input type="submit" button. <br> Can be used in the following elements: <br> ·BODY, FRAMESET, window <br> Platform support: <br> IE4 /Win, Mozilla 1.7a, Netscape 7.2, Firefox0.9 <br> Example: <br></p>
<div class="codetitle">
<span><a style="CURSOR: pointer" data="88705" class="copybut" id="copybut88705" onclick="doCopy('code88705')"><u>Copy code</u></a></span> The code is as follows:</div>
<div class="codebody" id="code88705"> <br> <html xmlns="http://www.w3.org/ 1999/xhtml"> <br> <head> <br> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <br> <title>onbeforeunload Test</title> <br> <script> <br> function checkLeave(){ <br> event.returnValue="Are you sure to leave the current page?"; <br> } <br> </script>