[英]how to change url without changing browser history
Until now, I only know that if I want to change URL without reloading the whole page i have to use HTML browser history API. 到现在为止,我只知道如果我想在不重新加载整个页面的情况下更改URL,我必须使用HTML浏览器历史记录 API。
I am using this concept in my website. 我在我的网站上使用这个概念。 Let's take example.
我们举个例子吧。 Let suppose user is on this page
假设用户在此页面上
https://www.example.com/aboutus
and then he goes to product listing in which we have filters. 然后他进入我们有过滤器的产品列表。 On clicking any filters system generates new url something like this
点击任何过滤器系统会生成类似这样的新网址
https://www.example.com/products?brands[]=song&brands[]=samsung&condition[]=new
Internally it is just calling 在内部,它只是在呼唤
history.pushState({}, 'Title', link.href);
But, it has one problem. 但是,它有一个问题。 Clicking
back
button takes to the previous filter. 单击
back
按钮将转到上一个过滤器。 I dont want this functionality . 我不想要这个功能 。 I want , on clicking browser back button it should take to the page which is before
current
page. 我想,点击浏览器后退按钮,它应该到
current
页面之前的页面。 In our case, it is suppose to take to 在我们的例子中,它应该采取
https://www.example.com/aboutus
Thanks. 谢谢。
You're looking for replaceState()
, it replaces the current position in the history instead of pushing a new one, like pushState()
does 你正在寻找
replaceState()
,它取代了历史中的当前位置而不是推新的位置,比如pushState()
history.replaceState()
operates exactly likehistory.pushState()
except thatreplaceState()
modifies the current history entry instead of creating a new one.history.replaceState()
运行方式与history.pushState()
完全相同,只是replaceState()
修改当前历史记录条目而不是创建新条目。
replaceState()
is particularly useful when you want to update the state object or URL of the current history entry in response to some user action.当您想要更新当前历史记录条目的状态对象或URL以响应某些用户操作时,
replaceState()
特别有用。
Remember, some functions are not available on older browsers . 请记住, 某些功能在旧版浏览器中不可用 。 But there is a library that could help you out.
但是有一个图书馆可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.