微信公众号开发之返回按钮事件监听

xiaoxiao2021-02-28  36

1.微信公众号交互页面嵌入了JSP页面,微信内置的浏览器左上角有返回按钮,而安卓版没有,在实际项目中,有页面跳转A-->B-->C,C页面做完处理点击返回直接跳到A页面的需求,默认的返回是不可以的,处理逻辑如下:

$(function(){     pushHistory();      window.addEventListener("popstate", function(e) {  //回调函数中实现需要的功能 // alert("我监听到了浏览器的返回按钮事件啦"); window.location.href='/wechart/contractList?userId=${userId}&openId=';  //在这里指定其返回的地址     }, false); });function pushHistory() {     var state = {         title: "我的合同",         url: "#"     };     window.history.pushState(state, state.title, state.url);

}

2.为了避免C页面跳回A页面后,再点击返回又回到C页面,我们可以再在A页面中监听下返回按钮,如果点击返回就关闭页面

$(function(){     pushHistory();      window.addEventListener("popstate", function(e) {  //回调函数中实现需要的功能 // alert("我监听到了浏览器的返回按钮事件啦"); WeixinJSBridge.call('closeWindow');     }, false); });function pushHistory() {          var state = {              title: "我的合同",              url: "#"         };          window.history.pushState(state, state.title, state.url);      }

转载请注明原文地址: https://www.6miu.com/read-2625391.html

最新回复(0)