You can use the onbeforeunload event to detect if the current tab is closed or the browser is closed.
Here's an example of how to use onbeforeunload with the Vue 3 Composition API in TypeScript:
index.ts354 chars18 linesIn this example, onBeforeUnmount is used to remove the event listener when the component is unmounted. When the component is mounted, window.addEventListener is called with the 'beforeunload' event and the handleUnload function. When the user tries to close the tab or the browser, the handleUnload function will be called and the message "Tab or browser is closed!" will be logged in the console.
Note that onBeforeUnmount and window.removeEventListener require Vue 3 Composition API to work.
gistlibby LogSnag