window.history.replaceState 没有替换浏览器历史列表

问题描述 投票:0回答:1

我正在使用

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>

如何更改浏览器历史列表/选项卡数据?

javascript html jquery replace browser-history
1个回答
1
投票

上面的代码应该可以工作。你可以试试

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>
© www.soinside.com 2019 - 2024. All rights reserved.