我有一个带有可搜索表格的页面。我写了一个“返回”按钮返回上一页:
<a href="javascript:history.back()">Back</a>
比如我主页的网址是:
https://xxxxxx.com
我从主页 进入带有可搜索表格的页面(或任何其他页面) :https://xxxxxx.com/data
当我执行搜索时,URL 变为:https://xxxxxx.com/data?search=*sth*
当我点击“返回”按钮时,URL 变为:https://xxxxxx.com/data
我真正想要的是:https://xxxxxx.com/
(或者用户在/data
之前进入的页面)如何在搜索按钮上重写我的 javascript,以便后退按钮可以到达实际页面?
最佳答案
而不是 history.back()
,只能回溯一个,你可以做 window.location.replace(window.location.host)
,它加载根页面。
获取更多信息 window.location
: https://developer.mozilla.org/en-US/docs/Web/API/Window/location
编辑:
如果要忽略 URL 参数,则需要更改转到另一个页面的方式。
而不是 window.history.pushState(...)
, 你应该使用 window.history.replaceState(...)
,当导航应该在后退单击时被忽略时。
这里有更多关于该主题的阅读:What is the trade off between history push and replace?
关于javascript - 无论查询字符串发生什么变化,都返回上一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63447306/