JavaScript用哈希值重新加载页面

我正在尝试通过一些过程在外部javascript文件中使用此语句刷新页面。

window.location.href = window.location.href

它完美地重新加载了页面,但是重新加载后我想滚动到特定的位置。因此,我将其放在页面上。

<a name="uploadImgSection"></a>

并将javascript更改为

window.location.href = window.location.href + "#mypara";

此代码也不会重新加载/刷新页面

window.location.hash = "#uploadImgSection";

window.location.href = window.location.href;

当我这样做时,它根本不会重新加载页面。有什么办法可以用滚动条位置重新加载页面?

回答:

window.location.href += "#mypara";

location.reload();

首先添加哈希,然后重新加载页面。哈希将保留在那里,并且页面将被重新加载。

经过测试,有效。


如果网址中已经存在哈希,则可能需要直接通过location.hash而不是进行更改.href

以上是 JavaScript用哈希值重新加载页面 的全部内容, 来源链接: utcz.com/qa/398845.html

回到顶部