Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does javascript listen for page refresh and page close events

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report