使用JavaScript检测对Android后退按钮的使用 [英] Detect use of Android back button using JavaScript

查看:183
本文介绍了使用JavaScript检测对Android后退按钮的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站有面包屑,突出显示了用户到达过程的哪个阶段.面包屑依赖于浏览器的历史记录,以告知使用浏览器后退按钮时应突出显示哪个阶段,但是在使用硬件后退按钮的Android设备上,这似乎已被绕过,突出显示的面包屑不会更改.

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.

我的网站没有使用PhoneGap或类似的东西,因为它通常不是移动网站,因此可以捕获Android后退按钮的使用,以便我可以添加事件以根据历史记录设置面包屑突出显示使用按钮,仅使用JavaScript还是jQuery?

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?

推荐答案

1.使用 popstate 事件.

1. Use popstate event.

https://developer.mozilla.org/zh-CN/docs/Web/Events/popstate

window.onpopstate = function(e) { 
   updateBreadCrumbObservable();
};

2.使用 onhashchange 事件.

2. Use onhashchange event.

window.onhashchange = function(e) {
   updateBreadCrumbObservable();
}

您可以使用事件参数来获取有关触发事件的更多描述.

You can use event argument to get more description about the event fired.

这篇关于使用JavaScript检测对Android后退按钮的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆