使用 NavigationHandler.handleNavigation 不显示页面顶部

Using NavigationHandler.handleNavigation doesn't show the top of the page

我正在按照此处的建议使用 NavigationHandler.handleNavigation (ExternalContext.dispatch() not working),因为我使用的是 ajax 请求。

它有效,但我在页面中间看到下一页(或多或少),而不是在页面顶部看到它。

我尝试按照此处的建议使用锚点 (http://www.computerhope.com/issues/ch001475.htm),但它也不起作用。

知道发生了什么吗?

这是我的代码:

context.getApplication().getNavigationHandler().handleNavigation(context, null, "/user-registration.xhtml#top");

我在下一页正文的开头添加了以下内容:

<a name="top"></a>

最后我用下面的脚本解决了这个问题:

<script type="text/javascript">
  $(document).ready(function() {
    location.hash = "#top";
  });
</script>

将 window 滚动到顶部的正确方法是 window.scrollTo() with xy of 0:

window.scrollTo(0, 0);

这可以让您的 URL 摆脱杂乱无章的哈希片段。

为了在每个 JSF ajax 事件成功时调用它,请在文档中包含以下脚本。

jsf.ajax.addOnEvent(function(data) {
    if (data.status == "success") {
        window.scrollTo(0, 0);
    }
});

另请参阅:

  • Execute JavaScript after every JSF ajax postback