ios系统可以正常监听,安卓手机用户必须与浏览器有交互操作,例如:先点击一下屏幕,popstate才能监听到返回键,解决方法如下:
判断手机系统为安卓,进入页面时执行代码window.tbs_bridge.nativeExec(“network”,“type”,0,null)
具体代码如下:
var isAndroid=navigator.userAgent.indexOf(“Android”)>-1||navigator.userAgent.indexOf(“Adr”)>-1;
var isIos=navigator.userAgent.match(/(i[^;]+;( U;)? CPU.+Mac OS X/);
history.pushState(null,null,document.URL);//最好加这行,有没有都行
window.addEventListener(“popstate”,this.popFun,false);
if(isAndroid){
try{
//安卓直接使用popstate监听返回键无效,需要加这行代码
window.tbs_bridge.nativeExec(“network”,“type”,0,null)
}catch(e){
console.log(e)
}
},
popFun(){
console.log(“popFun”)
}
使用popstate监听浏览器返回键
于 2021-12-31 23:03:26 首次发布