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
Add a ment  | 

2 Answers 2

Reset to default 3

1. 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