3

如何监听浏览器中同一域名的 tab 全部关闭?

 1 year ago
source link: https://www.v2ex.com/t/873662
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  JavaScript

如何监听浏览器中同一域名的 tab 全部关闭?

  Jexxie · 2 小时 18 分钟前 · 594 次点击

奇葩需求:所有这个网站的标签页 tab 全部关闭则退出登陆(调用 logout 接口)。

(以下代码是目前的实现,完全关闭浏览器后退出登陆)

 // 刷新和关闭网页都会触发 onunload, 因此用 sessionStorage 区分,浏览器完全关闭后会清空 sessionStorage
        sessionStorage.setItem('reloaded', 'yes');
        window.onunload = function(e) {
            if (sessionStorage.getItem('reloaded') == null) {
                fetch("/logout", {
                method: "POST",
                // keepalive 属性用于页面卸载时,告诉浏览器在后台保持连接,继续发送数据。开启了 keepalive 属性后,网页就算被关闭了,请求被会继续执行而不会中断。
                keepalive: true
            });
            }
        };

请问,有没有方法实现检测到所有同域标签页关闭的方法?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK