简体   繁体   English

如何在不重定向用户的情况下更改地址栏URL(不使用History API?)

[英]How to change address bar URL without redirecting the user (not using History API?)

I know it's a very common question around here. 我知道这是一个非常普遍的问题。 But I can't find answer that I'm looking for. 但是我找不到想要的答案。

I need to achieve a very similar behaviour like Groupon.com does it. 我需要实现类似Groupon.com这样的行为。 For instance, when you visit https://www.groupon.com/goods/all . 例如,当您访问https://www.groupon.com/goods/all时 On the left side you will see an " Interest " filtering widget and when you select one or more option, the page URL on the address bar will change to something like https://www.groupon.com/goods/all?interest=good-for-gifting . 在左侧,您将看到一个“ 兴趣 ”过滤小部件,当您选择一个或多个选项时,地址栏上的页面URL将更改为https://www.groupon.com/goods/all?interest=很好的礼物

In first place I thought that they are using the History API but they don't. 首先,我认为他们使用的是History API,但实际上并未使用。 Because when I check the history.length to see if it get increased, it always returns the same value. 因为当我检查history.length来查看它是否增加时,它总是返回相同的值。

Anyone knows how to do this? 有人知道该怎么做吗?

It maybe because they use replaceState() method which does not add any entry to the history but replaces the current entry 可能是因为他们使用了replaceState()方法,该方法不会向历史记录添加任何条目,但会替换当前条目

https://developer.mozilla.org/en-US/docs/Web/API/History_API https://developer.mozilla.org/zh-CN/docs/Web/API/History_API

Why don't you try URL Rewriting in htaccess or configuration files. 为什么不尝试在htaccess或配置文件中进行URL重写。 You can refer below article. 您可以参考下面的文章。 http://webrewrite.com/url-rewriting-htaccess/ http://webrewrite.com/url-rewriting-htaccess/

Hope this is useful! 希望这是有用的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM