how to using js detect browser window tab close event All In One
how to using js detect browser window tab close event All In One
JavaScript 中的一些加载,
卸载
事件:
- 页面
加载
时只执行 onload
事件; - 页面
关闭
时,先 onbeforeunload
事件,再 onunload
事件; - 页面
刷新
时先执行 onbeforeunload
事件,然后 onunload
事件,最后 onload
事件;
window.addEventListener('load', (event) => {
console.log('page is fully loaded');
});
window.addEventListener('DOMContentLoaded', (event) => {
console.log('DOM fully loaded and parsed');
});
demos
// tab
window.addEventListener('beforeunload', () => {
const params = {
user: this.username ?? '',
group: this.group ?? '',
modules: '',
path: '',
host: window.location.host,
os: this.os,
desc: 'tab 关闭✅',
};
params.modules = newPageName ?? '';
params.path = newPathname;
wxRobot(params, 'tab 关闭', true);
// setTimeout(() => {
// window.removeEventListener('beforeunload');
// }, 0);
});
const wxRobot = (params = {}, desc = '', clearEvent = false) => {
// 企业微信-robot
// const url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxyyyzzz';
const fetchPOST = (url, obj) => {
return fetch(
url,
{
method: 'POST',
mode: 'no-cors',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(obj),
},
).then(res => res.json()).then(json => json).catch(err => console.log('fetch error', err)).finally(() => {
if(clearEvent) {
window.removeEventListener('beforeunload');
}
});
};
const obj = {
msgtype: 'markdown',
markdown: {
content: `
> tab 关闭 App 数据上报\n
desciption: ${desc}\n
data: ${JSON.stringify(params, null, 4)}\n
`,
},
};
fetchPOST(url, obj);
};
// wxRobot(params, desc);
Web API
MDN
addEventListener('pageshow', (event) => { });
onpageshow = (event) => { };
https://developer.mozilla.org/en-US/docs/Web/API/Window/pageshow_event
addEventListener('pagehide', (event) => { });
onpagehide = (event) => { };
https://developer.mozilla.org/en-US/docs/Web/API/Window/pagehide_event
addEventListener('DOMContentLoaded', (event) => {});
onDOMContentLoaded = (event) => { };
https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event
addEventListener("load", (event) => {});
onload = (event) => {};
https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event
addEventListener('beforeunload', (event) => { });
onbeforeunload = (event) => { };
https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
addEventListener('unload', (event) => { });
onunload = (event) => { };
https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event
潜在 bug 问题❌
https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event#usage_notes
refs
https://juejin.cn/post/6844903871668551687
https://segmentfault.com/a/1190000016874879
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/14627068.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2020-04-07 taro render html
2020-04-07 TDD & Unit testing
2020-04-07 Taro 版本
2020-04-07 小程序 webview 自动打开新页面
2020-04-07 Taro Next
2020-04-07 taro 渲染 html
2019-04-07 Algorithm Visualizer