原理:在頁面中我們可以使用javascript window history,後退到前面頁面,但是由于安全原因javascript不允許修改history裡已有的url連結,但可以使用pushState方法往history裡增加url連結,并且提供popstate事件監測從history棧裡彈出url。既然有提供popstate事件監測,那麼我們就可以進行監聽。
具體實作代碼如下:
$(function(){
pushHistory();
window.addEventListener("popstate", function(e) {
alert("我監聽到了浏覽器的傳回按鈕事件啦");//根據自己的需求實作自己的功能
}, false);
function pushHistory() {
var state = {
title: "title",
url: "#"
};
window.history.pushState(state, "title", "#");
}
});
關閉浏覽器:
//關閉微信頁面
function weixinClosePage() {
if (typeof WeixinJSBridge == "undefined") {
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', weixin_ClosePage, false);
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', weixin_ClosePage);
document.attachEvent('onWeixinJSBridgeReady', weixin_ClosePage);
}
} else {
weixin_ClosePage();
}
}
function weixin_ClosePage() {
WeixinJSBridge.call('closeWindow');
}
直接在你需要傳回自定義菜單的頁面中執行weixinClosePage()就行。