如果所有在url中更改的内容都是散列,如何强制页面重新加载? [英] How to force a page to reload if all what was changed in url is hash?
问题描述
(说明我希望如何工作:重新加载然后滚动到新的散列。)
方法#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;
有用,但我宁愿不向网址添加随机垃圾。
是否有更好的解决方案?
删除要导航到的锚点,然后使用方法#2?由于没有锚,因此设置散列不应滚动页面。
I am trying to reload current page with different url hash, but it doesn't work as expected.
(Clarification how I want it to work: Reload the page and then scroll to the new hash.)
Approach #1:
window.location.hash = "#" + newhash;
Only scrolls to this anchor without reloading the page.
Approach #2:
window.location.hash = "#" + newhash;
window.location.reload(true);
Kinda works but it first scrolls to the anchor, then reloads the page, then scrolls to the anchor again.
Approach #3:
window.location.href = window.location.pathname + window.location.search + "&random=" + Math.round(Math.random()*100000) + "#" + newhash;
Works but I would rather not add random garbage to the url.
Is there a better solution?
Remove the anchor you're going to navigate to, then use approach #2? Since there's no anchor, setting the hash shouldn't scroll the page.
这篇关于如果所有在url中更改的内容都是散列,如何强制页面重新加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!