我正在尝试使用不同的 url 哈希重新加载当前页面,但它没有按预期工作。
(澄清我希望它如何工作:重新加载页面,然后滚动到新的哈希。)
方法#1:
window.location.hash = "#" + newhash;
仅滚动到此锚点而不重新加载页面。
方法#2:
window.location.hash = "#" + newhash;
window.location.reload(true);
有点工作,但它首先滚动到锚点,然后重新加载页面,然后再次滚动到锚点。
方法#3:
window.location.href = window.location.pathname + window.location.search + "&random=" + Math.round(Math.random()*100000) + "#" + newhash;
有效,但我宁愿不向 url 添加随机垃圾。
有更好的解决方案吗?