简体   繁体   English

如何更改网址而不更改浏览器历史记录

[英]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()

from MDN 来自MDN

history.replaceState() operates exactly like history.pushState() except that replaceState() 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.

相关问题 如何在不更改历史记录的情况下更改浏览器网址 - How to change browser url without changing anything in history 添加到浏览器历史记录而不改变当前 URL - Add to browser History without changing current URL 该网站如何在不更改页面的情况下更改浏览器的URL? - How does this site change the URL of the browser without changing pages? 无需导航即可更改路由器 URL,但会添加到浏览器历史记录 - Change router URL without navigating, but add to browser history React Router更改路由/组件而不更改浏览器URL - React Router to change route/component without changing browser URL 将历史记录条目添加到浏览器而不更改散列或URL的任何其他部分 - Add history entry to the browser without changing the hash or any other parts of the URL 如何强制浏览器在不更改URL的情况下重新加载缓存的URL - How to force the browser to reload a cached URL without changing the URL 如何在不更改主干页面的情况下更改url - How to change url without changing page in backbone 如何在不重新加载页面的情况下更改浏览器 URL? - How to change the browser URL without reload the page? 如何在不重新加载的情况下更改浏览器栏网址 - How to change browser bar url without reload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM