Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does javascript listen for page refresh and page close events

Shulou Source: shulou.com Published: 2022-05-31 11:52:42 09月23日 Update

This article introduces the knowledge of "how javascript monitors page refresh and page closure events". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In our daily life, we often encounter such a situation, when we click on a link, close the page, form submission and so on, we will be prompted whether to confirm the operation and other information.

Here I will tell you about the onbeforeunload () and onunload () events of javascript.

Similarities:

Both are operating on the closing or refreshing event of the page.

Differences:

The order in which the unbeforeunload () event executes occurs before the onunload () event. Because unbeforeunload () is an event that is triggered before the page is refreshed, while onubload () is triggered after the page is closed.

The unbeforeunload () event can disable the firing of the onunload () event.

The onunload () event does not prevent the page from closing.

Compatibility of browsers

Onunload:

Refresh the page in IE6,IE7,IE8, close the browser, and execute after the page jumps.

IE9 refresh page will be executed, page jump, closed browser cannot be executed

Firefox (including firefox3.6) can execute after closing the tag, after the page jumps, and after refreshing the page, but closing the browser cannot be executed.

Safari refreshes the page and executes after the page jumps, but cannot be executed when the browser is closed

Opera and Chrome will not be executed under any circumstances.

Onbeforeunload:

Perfect support for IE, Chrome and Safari

Firefox does not support text reminder messages

Opera does not support

Bug will appear in IE6,IE7

Sample code:

Onbeforeunload ():

Method 1: add to the html element

Method 2: add javascript

_ window.onunload = function () {/ / dosomethings}; that's all for "how javascript listens for page refresh and page closure events". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Event page element way browser browse situation link support content instance is in demo monitor thing information address tutorial more knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MySQL MariaDB Shulou Technology Docker