87,990
社区成员
发帖
与我相关
我的任务
分享
window.onload = function () {
document.getElementById('btn').onclick = function () {
let a = location.hash;
if (a.indexOf('tyjlyy') < 0) {
window.history.replaceState({ page: 3 }, '', ''); // 假想 page1 修改page1的state的值
console.log(history.state);//page:3 口
window.history.pushState({ nei: 1 }, '', location.href + '#ty') //假想 page2 直接跳转page2
console.log(history.state);//nei:1 口口
}
}
}
window.addEventListener("popstate", function (e) { //触发浏览器的前进 返回
let a = location.hash;
if (a.indexOf('ty') < 0) {
//当前为 page2
//返回 则取上一条历史记录(page1)所携带的参数 即page:3
console.log(e.state);
// alert("请勿离开"); 虽然提示 但点确定后最后还是会离开
} else {
//当前为 page1
//前进 则取上一条历史记录(page2)的所携带的参数
console.log(e.state);
}
}, false);
楼上概念说的基本差不多了
这是我个人理解
好乱.........
