admin管理员组文章数量:1277555
My site has breadcrumbs which highlight which stage in a process the user has reached. The breadcrumbs rely on the browser history to tell which stage should be highlighted when the browser back button is used, but on Android devices using the hardware back button this seems to be bypassed and the highlighted breadcrumb does not change.
My site is not using PhoneGap or anything similar as it's not usually a mobile site, so is it possible to capture use of the Android back button so that I can add an event to set the breadcrumb highlight based on the history log when the button is used, just using JavaScript or jQuery?
My site has breadcrumbs which highlight which stage in a process the user has reached. The breadcrumbs rely on the browser history to tell which stage should be highlighted when the browser back button is used, but on Android devices using the hardware back button this seems to be bypassed and the highlighted breadcrumb does not change.
My site is not using PhoneGap or anything similar as it's not usually a mobile site, so is it possible to capture use of the Android back button so that I can add an event to set the breadcrumb highlight based on the history log when the button is used, just using JavaScript or jQuery?
Share Improve this question edited Jun 17, 2018 at 21:37 Grokify 16.4k8 gold badges69 silver badges92 bronze badges asked Dec 4, 2013 at 11:15 CaraselCarasel 2,8715 gold badges33 silver badges52 bronze badges 5- 4 I haven't tried anything. I just want to know if it is possible to capture the device back button, and if so, how to do it. I don't know of anything to try, as I don't know if it is even accessible to javascript. – Carasel Commented Dec 4, 2013 at 11:44
- Check this link: stackoverflow./a/2000319/1739882 – Chintan Soni Commented Dec 4, 2013 at 11:53
- Thanks, but this is not an android app, it is just a website, so I am looking for a way to detect the back button without using Java. – Carasel Commented Dec 4, 2013 at 11:55
- Ah, I followed the thread of SO answers and eventually got to this one: stackoverflow./questions/136937/… – Eric L. Commented Mar 13, 2014 at 16:53
- 1 The browser will catch the android back button event and fire popstate. But we wouldn't know if all popstate events e from android back button, or there is also a back button in the broswer. – Xuezheng Ma Commented Jun 18, 2018 at 21:46
2 Answers
Reset to default 31. Use popstate
event.
https://developer.mozilla/en-US/docs/Web/Events/popstate
window.onpopstate = function(e) {
updateBreadCrumbObservable();
};
2. Use onhashchange
event.
window.onhashchange = function(e) {
updateBreadCrumbObservable();
}
You can use event argument to get more description about the event fired.
You can put check on inside onBackPressed method of the activity and before calling super.onBackPressed(); trigger the method to call javascript method.
for e.g:
override fun onBackPressed() {
if (handleBackPress) {
myWebView.loadUrl("javascript:backButtonPressed()")
}else {
super.onBackPressed()
}
}
in the above example if handleBackPress boolean variable is true, then it will try to call backButtonPressed() method of javascript file in the webview.
Let me know if you need any explanation or help.
本文标签: Detect use of Android back button using JavaScriptStack Overflow
版权声明:本文标题:Detect use of Android back button using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741219504a2360695.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论