我正在使用
window.history.replaceState
替换新 URL 中的 URL,它部分工作正常。
当有人访问
example.com/?&test=123
时,它将用 example.com/?
替换 URL 并且后退按钮不会转到以前的 URL(后退按钮工作正常)
但是当我在浏览器(chrome 或 firefox)中看到历史选项卡时,我可以看到两个历史记录,一个是用
example.com/?&test=123
创建的,另一个是 example.com/?
但它应该只有一个条目 example.com/?
<script type="text/javascript">
var url = window.location.href;
var newUrl = url.replace(/&test=[^&]*/, '');
window.history.replaceState({}, '', newUrl);
</script>
如何更改浏览器历史列表/选项卡数据?
上面的代码应该可以工作。你可以试试
window.history.go(-1)
<script type="text/javascript">
var url = window.location.href;
var newUrl = url.replace(/&test=[^&]*/, '');
window.history.pushState({}, '', newUrl);
window.history.go(-1);
</script>
或
<script type="text/javascript">
var url = window.location.href;
var newUrl = url.replace(/&test=[^&]*/, '');
window.history.replaceState({}, '', newUrl);
window.history.go(-1);
</script>