Here You can use Easy way pagehide. The pagehide
event is sent to a Window
when the browser hides the current page in the process of presenting a different page from the session's history.
For example, when the user clicks the browser's Back button, the current page receives a pagehide
event before the previous page is shown.
- A mobile user visits your page.
- The user then switches to a different app.
- Later, the user closes the browser from the app manager.
Examples
In this example, an event handler is established to watch for pagehide
events and to perform special handling if the page is being persisted for possible reuse.
window.addEventListener("pagehide", event => {
if (event.persisted) {
/* the page isn't being discarded, so it can be reused later */
}
}, false)