Project needs: I also found a lot of similar articles on the Internet, but there are some problems in using them. After all, it has to suit your own needs. I am using vue3 here, but in theory vue2 can also be used. The method I wrote is universal. These methods are all executed based on 1. beforeunload event1.1、Novice tutorial: 1.2、MDN 2. Unload event2.1、Novice Tutorial 2.2、MDN
The source code I used is posted below; 3. Source code3.1. Method 1: Can be written in HTML page (direct use) var _beforeUnload_time = 0, _gap_time = 0; window.onunload = function (){ _gap_time = new Date().getTime() - _beforeUnload_time; if(_gap_time <= 10) {//Browser close window.mgr.signoutRedirect();//This mgr is the logout method I exposed in window}else{//Browser refresh - chrome refresh console.log(document.domain); return confirm("Are you sure you want to leave this system?"); } }; window.onbeforeunload = function (){ _beforeUnload_time = new Date().getTime(); }; 3.2. Method 2: Can be written in components such as data() { return { gap_time: 0, beforeUnload_time: 0, }; }, methods: { //Execute before closing the window beforeunloadHandler() { this.beforeUnload_time = new Date().getTime(); }, unloadHandler() { this.gap_time = new Date().getTime() - this.beforeUnload_time; //Judge whether the window is closed or refreshed in milliseconds. Most of the online readings are 5 if (this.gap_time <= 10) { mgr.signoutRedirect(); // Logout interface should be replaced with personal logout method} else { console.log(document.domain); return confirm("Are you sure you want to leave this system?"); } }, }, unmounted() {//vue can be replaced with destroyed() life cycle, but this can also be used // Remove the listener window.removeEventListener("beforeunload", () => this.beforeunloadHandler()); window.removeEventListener("unload", () => this.unloadHandler()); }, mounted() { // Listen for browser closing window.addEventListener("beforeunload", () => this.beforeunloadHandler()); window.addEventListener("unload", () => this.unloadHandler()); }, Reference articles: This is the end of this article about the implementation of closing the browser and logging out in vue. For more relevant vue closing the browser and logging out content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Analysis of the Principle and Function of MySQL Database Master-Slave Replication
>>: Detailed example of installing FastDfs file server using docker compose
Table of contents 1. Project folder structure 1. ...
Build the image Earlier we used various images fo...
aforementioned This article is very short~ The ma...
Table of contents 1. Anonymous slots 2. Named slo...
Effect: css: .s_type { border: none; border-radiu...
As more and more projects are deployed, more and ...
1. Multi-header table code Copy code The code is a...
This article shares the specific code of JavaScri...
Preview: Code: Page Sections: <template> &l...
Table of contents 1. Purpose 2. Grammar 3. Practi...
Mysqldump is used for logical backup in MySQL. Al...
Table of contents background Target Effect Ideas ...
MySQL 5.7.8 introduced the json field. This type ...
Introduction Recently I found that there is an AR...
MySQL tuning Explain tool detailed explanation an...