init() {
window.history.pushState({ target: "MeanSure", random: Math.random() }, "", location.href);
window.history.pushState({ target: "Final", random: Math.random() }, "", location.href);
const option = {
closeOnPopstate: false,
confirmButtonText: "留下",
cancelButtonText: "离开",
message: "确认离开当前页面吗?"
};
async function observer(e) {
// console.log("popstate", e?.state);
if (e.state?.target == "MeanSure") {
try {
await Dialog.confirm(option);
stay();
} catch (error) {
back();
}
}
}
//封装的常规操作,回退到上一级
function back() {
// console.log("back", history?.state?.target);
let backCount = history.state.target == "Final" ? -3 : -2;
history.go(backCount);
}
//封装的常规操作,停留在本页面
function stay() {
// console.log("stay", history?.state?.target);
history.go(1);
}
window.onpopstate = observer;
}
网友评论