2017-10-11 90 views
0

我有一個帶有側邊欄菜單的移動/網絡應用程序。我試圖複製原生應用程序所具有的相同行爲。當側欄菜單打開並且用戶點擊後退(android)時,我想避免使用history.back(),所以URL永遠不會改變,還有一些回調讓我關閉菜單。捕獲history.back()並更改行爲

這可能嗎?或者至少有漸進的網絡應用程序?

回答

0

嘗試onpopstate捕獲它:

window.onpopstate = function(event) { 
    alert("location: " + document.location + ", state: " + JSON.stringify(event.state)); 
}; 

查看更多here

+0

但是是不可能的preventDefault。 https://stackoverflow.com/a/32432366/1469219 – EnZo

+1

@EnZo你必須推動一些東西在菜單打開,例如:'window.history.pushState(null,null,window.location.href)' – tenbits