背影:再做移动端一个微信商城的时候,有一个会员升级页面,点击升级之后进行微信支付,支付成功之后会跳转到定制的一个支付成功页面,此时点击浏览器的返回,就会返回到微信的支付页面,然后提示已经支付成功,一般来说,这里需要后退到 升级页面或者主页,那么就需要监听浏览器的返回事件。
//将如下代码加入到支付成功的定制页面中
$(function () {
pushHistory();
var bool = false;
setTimeout(function () {
bool = true;
}, 1500);
window.addEventListener("popstate", function (e) {
if (bool) {
window.history.go(-2); //此处 相当于连续返回两下,跳过 支付页得history
}
}, false);
function pushHistory() {
var state = {
title: "title",
url: "#"
};
window.history.pushState(state, "title", "#");
}
});