gpt4 book ai didi

javascript - 不使用 location.replace 从历史浏览器中删除当前页面

转载 作者:行者123 更新时间:2023-11-30 21:21:30 33 4
gpt4 key购买 nike

我有一个页面 example1.aspx 调用另一个页面 example2.aspx?id=5
example2.aspx 中,我使用 id (5) 并执行 ajax 请求。
我不喜欢显示 ?id=5,所以我使用

history.pushState("", document.title, window.location.pathname);
删除 window.location.search
它运行良好,但我的问题是,如果用户单击后退按钮,浏览器将重定向到 example2.aspx?id=5。如何从浏览器历史记录中删除页面 example2.aspx?id=5

我阅读了很多建议使用 location.replace 的其他问题的答案,但这对我没有帮助,因为如果我使用 location.replace("example2.aspx") 浏览器刷新页面,刷新时页面,如果没有?id 自动执行id=1 的 ajax 请求。

那么,是否可以在不使用 location.replace 的情况下从历史记录中删除页面?

最佳答案

所有你想要的,History有:

  var stateObj = { foo: "bar" };
history.replaceState(stateObj, "page 2", "bar.html");

关于javascript - 不使用 location.replace 从历史浏览器中删除当前页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45139138/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com