1

I am using NavigationHandler.handleNavigation as suggested here (ExternalContext.dispatch() not working) since I am using an ajax request.

It works but I see the next page in the middle of the page (more or less) instead of seeing it in the top of the page.

I tried using an anchor as suggested here (http://www.computerhope.com/issues/ch001475.htm) but it also doesn't work.

Any idea of what is happening?

Here is my code:

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

I have added the following in the beginning of the body of the next page:

<a name="top"></a>
Community
  • 1
  • 1
Aliuk
  • 1,249
  • 2
  • 17
  • 32

2 Answers2

1

The correct approach to scroll the window to top is window.scrollTo() with x and y of 0:

window.scrollTo(0, 0);

This keeps your URL free of hash fragment clutter.

In order to invoke it on success of every JSF ajax event, include the following script in the document.

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

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Finally I solved it with the following script:

<script type="text/javascript">
  $(document).ready(function() {
    location.hash = "#top";
  });
</script>
Aliuk
  • 1,249
  • 2
  • 17
  • 32