更改查询字符串而不重新加载/刷新


Change querystring without reloading/refreshing

我想在不刷新页面的情况下更改我的网址。例如,我的网址是http://test.com?search=test

当我单击搜索页面上的任何过滤器值(如"类别")时,我的 URL 应该是:

http://test.com?search=test$category=cat1

当我单击另一个类别时,它应该是:

http://test.com?search=test&category=cat1,cat2

当我点击一个品牌时,URL应该是:

http://test.com?search=test$category=cat1,cat2&brand=br1

当我取消选择类别时,URL 应该是:

http://test.com?search=test$category=cat2&brand=br1

请帮助我。

您可以通过历史记录 API 进行更改

window.history.pushState("object or string", "Title", "/new-url");

在这种情况下,将使用新标题设置相对/new-url。您可以在控制台中进行试验。如果您在 stackoverflow.com 上输入此内容,则网址将神奇地变为 https://stackoverflow.com/new-url