8

I've got a gwt application and I want to scroll to the top of a page using this method:

public static native void scrollTop() /*-{
  $wnd.scroll(0, 0);
}-*/;

The method is called in the onClick-method of a TreeNodeListenerAdapter:

new TreeNodeListenerAdapter() {
  @Override
  public void onClick(Node node, EventObject e) {
    scrollTop();
  }
}

This does not work and I don't know why. When I put an alert inside my method:

$wnd.alert("Treenode clicked");

I get to see the alert but the page is not scrolled. What am I missing here?

xor_eq
  • 3,933
  • 1
  • 29
  • 33
  • 5
    I don't know why your code doesn't work, but you might consider using the GWT static method Window.scrollTo instead of your native method (see http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/user/client/Window.html). – aem Jul 07 '10 at 13:53
  • Thanks for the info, but that doesn't work either. – xor_eq Jul 07 '10 at 14:30
  • Weird stuff. I just tried Window.scrollTo and it worked fine in IE and FF4b1. What browser are you having issues with? – j flemm Jul 07 '10 at 20:15
  • 1
    Firefox 3.6 and ie6. A simple testpage with the Javascript and without all the gwt and ext-gwt stuff works fine in both browsers, I tried that. So I guess there is something else active that blocks my attempts to scroll the page :/ – xor_eq Jul 08 '10 at 06:52

1 Answers1

18

If you want to scroll to the top of a page just do:

Window.scrollTo (0 ,0);

Just be sure that you are importing the correct package com.google.gwt.user.client.Window

Andres
  • 1,484
  • 19
  • 29
  • I actually can't remember anymore what the solution to my problem was, but I solved it somehow. Since this should normally work, I'll accept it as answer. – xor_eq Feb 28 '11 at 22:19