JS可以监控手机的返回键吗?

html5的话

一进页面就pushState,然后监控onpopstate
不过好像没有办法知道是前进还是后退
我的奇淫巧计是,一个数字变量,pushState一个锚,锚是这个数字,前进一个页面数字+1,重新pushState这个变量.....onpopstate的时候判断如果锚的值小于全局变量.说明后退了.. dosomething
 
if (window.history && window.history.pushState) { $(window).on('popstate', function() { var hashLocation = location.hash; var hashSplit = hashLocation.split("#!/"); var hashName = hashSplit[1]; if (hashName !== '') { var hash = window.location.hash; if (hash === '') { alert('後退按鈕點擊'); } } }); window.history.pushState('forward', null, './#forward'); }



作者:Cherry
链接:https://www.zhihu.com/question/28223004/answer/148094159
来源:知乎

作者:万全
链接:https://www.zhihu.com/question/28223004/answer/158235597
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

var _this = this; var open_i = 0; pushHistory(); window.addEventListener("popstate", function(e) { if (//t/n/.test(location.href)) { location.href='//ch1'; // location.replace('//m.k.sohu.com?backMain=page1'); }else{ WeixinJSBridge.invoke('closeWindow',{},function(res){ //alert(res.err_msg); }); } // } }, false); function pushHistory() { var state = { title: "首页", url: "/t/ch1?backMain=ch1" }; window.history.pushState(state, state.title, state.url); window.history.replaceState(state, state.title, state.url); }
原文地址:https://www.cnblogs.com/xzzzys/p/8422913.html