Usually there are two methods to choose when the browser is closed: 1. onUnload 2. onbeforeunload
Because 1 is executed in different browsers and some are not executed, it is usually handled in the onbeforeunload event.
This event occurs in many situations:
- Refresh page
- PostBack
- Click link
- Browser navigation before and after operations
Sometimes you do not need to execute Close event in some steps such as clicking a link or refreshing, so we can use some methods to exclude these operations. Of course, we cannot detect the front and back of the navigation, and we cannot eliminate it.
Principle of exclusion:
Set some flag spaces in the operation that you do not want to execute the script to close the script, and then judge in the event. The simple logic is as follows:
$ ("A"). Click (function () {isclose = false ;});
Window. onbeforeunload = function () {If (isclose) {do ()}}