Before the browser is closed, I want to send data to the server that the user switches to the offline status.
After several days of studying the question, I have not found an answer.
How to track this event in vuejs/quasar framework?
Before the browser is closed, I want to send data to the server that the user switches to the offline status.
After several days of studying the question, I have not found an answer.
How to track this event in vuejs/quasar framework?
Getting idea from the same question in Vue.js forum, here is a possible solution:
import { createApp } from "vue";
import App from "./App.vue";
const app = createApp({
extends: App,
created() {
window.addEventListener("beforeunload", this.leaving);
},
methods: {
leaving() {
// Your code here
},
},
});
app.mount("#app");
You can play with the complete example on CodeSandbox.
Note:
Also tested with Firefox v99.0 and Chrome v101.0.4951.41 Desktop version.
On Chrome, the event won't be fired if the user did not interact with the page before closing.
Window:beforeunload
event may not be reliable on mobile.