How to determine whether it is closed or refreshed?

xiaoxiao2021-03-06  47

When we write JS code, it is often necessary to judge whether the web page is turned off. If it is turned off, it will execute a certain code. This can perform a JS code with HTML's onbeforeunload event, but if the web is only refreshed, Also triggers the onbeforeunload event, the following JS code can determine if it is closed

/ / Judgment is closed or refreshed

IF (Event.Clientx> Document.Body.Clientwidth && event.clienty <0 || Event.altKey)

{

Alert ("You turn off the browser");

}

This code is to determine whether the mouse is hitting the button, or pressing Alt F4 to shut down the web page, if so, if so, the system is to close the web page, otherwise the system is refreshing web page.

Ayuan

9CBS certified blog expert

Blog expert

Huawei old employees

Big Data

More than ten years, in Huawei, Internet Corporation's experience, profound understanding and practical experience in CRM, big data, mainly sharing various project experience, including architecture, Java, big data and other articles

转载请注明原文地址:https://www.9cbs.com/read-47574.html

New Post(0)