跳转到新页面并清除当前页面的history记录

History 接口允许操作浏览器的曾经在标签页或者框架里访问的会话历史记录,通过history方法触发前进后退
通常使用的方式:

window.location.href='index.html'
// 或者
window.location.replace('index.html')

使用场景:比如别人访问你得网站 不想回退的时候一步一步回,就需要避免访问你得页面的时候产生history历史记录。

解决办法:清除历史记录

window.history.replaceState()

对当前history记录清除:此方法只是直接屏蔽或者针对内网(同源)地址间的跳转,并不进行跳转。
需要搭配history.go(0)方法或者load方法进行页面跳转

使用用例:

// 跳转到user.html 页面并清除当前页面的history记录
window.history.replaceState(null, "", 'user.html');
window.history.go(0);

猜你喜欢

转载自blog.csdn.net/qq_27751965/article/details/115082765
今日推荐