[英]Refresh browser page on browser back click when changing hash value in url
I have a website with filters. 我有一个带有过滤器的网站。 I have appended hash whenever any filter is applied.
每当应用任何过滤器时,我都会附加哈希。 For example i have city, color filter.
例如我有城市,彩色滤光片。
URL/#color=red&city=Delhi URL /#color = red&city = Delhi
Now when someone clicks browser back on such url, it becomes URL/#color=red. 现在,当有人在该URL上单击浏览器时,它将变为URL /#color = red。 The city query string is removed by browser.
城市查询字符串被浏览器删除。
The problem is the page doesn't reload itself. 问题是页面不会重新加载自身。
$(window).on('hashchange', function () {
window.location.reload(true);
});
But this is infinitely loading the page. 但这无限地加载页面。
Try history API https://developer.mozilla.org/en-US/docs/Web/API/History_API 试用历史记录API https://developer.mozilla.org/zh-CN/docs/Web/API/History_API
eg 例如
var stateObj = { foo: "bar" };
history.pushState(stateObj, "page 2", "bar.html");
You may not want to hear this, but the HTML you have is not valid and needs to be fixed by converting the anchors #
to proper query strings ?
你可能不爱听这个,但你有HTML是无效的,需要通过转换锚固定
#
适当的查询字符串?
. 。 This will allow the browser to go back and reload the page.
这将允许浏览器返回并重新加载页面。
You can do this using a find and replace in your HTML editor. 您可以使用HTML编辑器中的“查找并替换”来执行此操作。
If that's going to cause problems to the way you've implemented your filters, you'll need to update your filtering code. 如果这将对实现过滤器的方式造成问题,则需要更新过滤代码。 If you post that here we can help fix that.
如果您在此处发布,我们可以帮助您解决。
Hope that helps. 希望能有所帮助。 :-)
:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.