我们如何使用Javascript在Android/iOS的Chrome手机浏览器中检测onTabClosed(事件)
我已经连续三天在研究如何在Chrome mobile Android上检测客户端何时关闭浏览器选项卡 我找不到任何工作代码:( 我使用三星Galaxy Note 10 Plus通过C型电缆与Chrome PC进行调试(chrome://inspect/#devices) 我使用了Android Studio的模拟器(谷歌像素) 两个设备的结果相同,当选项卡关闭时,不会触发任何内容 当我在Chrome PC上运行相同的代码时,它就工作了 我还使用:requestbin.com通过GET请求进行调试 以下是我所有的研究代码。如有任何建议或想法,将不胜感激我们如何使用Javascript在Android/iOS的Chrome手机浏览器中检测onTabClosed(事件),javascript,google-chrome,mobile,tabs,dom-events,Javascript,Google Chrome,Mobile,Tabs,Dom Events,我已经连续三天在研究如何在Chrome mobile Android上检测客户端何时关闭浏览器选项卡 我找不到任何工作代码:( 我使用三星Galaxy Note 10 Plus通过C型电缆与Chrome PC进行调试(chrome://inspect/#devices) 我使用了Android Studio的模拟器(谷歌像素) 两个设备的结果相同,当选项卡关闭时,不会触发任何内容 当我在Chrome PC上运行相同的代码时,它就工作了 我还使用:requestbin.com通过GET请求进行调试
函数左页(x){
var URL='1〕https://enf***********.m.pipedream.net/?test=true&x='+x+'&date='+(新日期()+)。替换(/\s/g');
console.log(“客户端已离开页面”,x);
fetch(URL,{method:“GET”});
$.ajax(URL{
async:true,
类型:'GET',//http方法
成功:功能(数据、状态、xhr){
console.log(“SENT1!”,x);
},
错误:函数(jqXhr、textStatus、errorMessage){
log(“ERROR1!”,x);
}
});
$.ajax(URL{
async:false,
类型:'GET',//http方法
成功:功能(数据、状态、xhr){
console.log(“SENT2!”,x);
},
错误:函数(jqXhr、textStatus、errorMessage){
log(“ERROR2!”,x);
}
});
document.getElementById(“输出”).innerHTML+=“Bam!”+x+”
;
警报(“砰!”+x);
}
addEventListener(“beforeunload”,函数(事件){
左侧页面(1);
返回null;
});
addEventListener(“beforeunload”,(事件)=>{
左侧页面(2);
返回null;
});
window.onbeforeunload=函数(事件){
左侧页面(3);
返回null;
};
window.onbeforeunload=(事件)=>{
左侧页面(4);
返回null;
};
addEventListener(“onunload”,(事件)=>{
左侧页面(5);
返回null;
});
addEventListener(“onunload”,函数(事件){
左侧页面(6);
返回null;
});
window.onunload=函数(事件){
左侧页面(7);
返回null;
};
window.onunload=(事件)=>{
左侧页面(8);
返回null;
};
$(窗口).on('beforeunload',function()){
左页(9);
//返回null;
});
$(窗口).on('beforeunload',()=>{
左侧页面(10);
//返回null;
});
$(窗口).on('unload',function()){
左侧页面(11);
返回null;
});
$(窗口).on('unload',()=>{
左侧页面(12);
返回null;
});
// https://developers.google.com/web/updates/2018/07/page-lifecycle-api#observing-代码中的页面生命周期状态
常量getState=()=>{
如果(document.visibilityState=='hidden'){
返回“隐藏”;
}
if(document.hasFocus()){
返回“活动”;
}
返回“被动”;
};
//使用“getState()”函数(上面定义)存储初始状态。
让state=getState();
//接受下一个状态,如果状态发生更改,则记录
//更改到控制台。它还更新上面定义的“state”值。
const logStateChange=(nextState)=>{
const prevState=状态;
if(nextState!==prevState){
//log(`State change:${prevState}>>>${nextState}`);
state=nextState;
如果(状态==“终止”){
左侧页面(13);
}
}
};
//这些生命周期事件都可以使用相同的侦听器来观察状态
//更改(它们调用`getState()`函数来确定下一个状态)。
['pageshow','focus','blur','visibilitychange','resume']{
addEventListener(类型,()=>logStateChange(getState()),{capture:true});
});
//另一方面,接下来的两个侦听器可以确定下一个侦听器
//事件本身的状态。
window.addEventListener('freeze',()=>{
//在冻结事件中,下一个状态始终处于冻结状态。
logStateChange(“冻结”);
},{capture:true});
window.addEventListener('pagehide',(事件)=>{
if(event.persistend){
//如果事件的持久化属性为'true',则页面将显示
//进入同样处于冻结状态的前向缓存。
logStateChange(“冻结”);
}否则{
//如果事件的持久化属性不是'true',则页面为
//即将卸车。
logStateChange(“终止”);
}
},{capture:true});
//
lifecycle.addEventListener('statechange',函数(事件){
//日志(event.oldState、event.newState);
如果(event.newState==“已终止”){
左侧页面(14);
}
});
测试onCloseTab()