antony.trupe
antony.trupe

Reputation: 10834

gwt anchor tag not anchoring

Why does the browser not scroll to the anchor?
url:http://localhost:8080/index.html#myAnchor3

this.anchor1.setName("myAnchor1");
this.add(this.anchor1);
this.anchor2.setName("myAnchor2");
this.add(this.anchor2);
this.anchor3.setName("myAnchor3");
this.add(this.anchor3);

Is it because the anchor is created after the page has finished loading, so the browser doesn't see the anchor when it tries to scroll to it?

Upvotes: 1

Views: 2116

Answers (3)

antony.trupe
antony.trupe

Reputation: 10834

Had to override the onLoad method, and call scrollIntoView there, otherwise it was trying to scroll to an object that wasn't added to the DOM yet.

public class Foo extends Widget
{
  Foo(){
  }

  @Override
  protected void onLoad(){
    super.onLoad();
    getElement().scrollIntoView();
  }
}

Upvotes: 1

Justin Fagnani
Justin Fagnani

Reputation: 11171

You could try using Element.scrollIntoView(), which not only will scroll the window, but any scrollable container in the DOM hierarchy that holds the element.

Upvotes: 1

o.k.w
o.k.w

Reputation: 25810

Try this:

this.anchor.setName("myAnchor");
this.add(this.anchor);
location.hash = '#myAnchor';

And yes, you are right, your anchor was created/inserted after the page load, so well.....

Upvotes: 1

Related Questions