html5的話html
一進頁面就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
來源:知乎
html5
做者:萬全
連接: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); }