5

I want to make my hyperlinks act like buttons (only respond to Click event) however, they change the history token automatically when you click on them, which messes up my history mechanism in my application. Is there a way to suppress the default behaviour of the hyperlink so it doesn't change the history token? I tried setting targetHistoryToken to null but it didn't work.

Thanks,

kapandron
  • 3,546
  • 2
  • 25
  • 38
  • I've got to ask, if it's going to act like a button, then shouldn't it be a button? Isn't there some user expectation about the behavior of your widget? – Glenn May 05 '09 at 17:36
  • Fair question. My boss loves the Google Reader interface where everything is lightweight (they use links for their actions). I suppose I can use a button and change the css to make it look like a link but I'm just wondering if I can do so with the hyperlink class itself. –  May 05 '09 at 17:44
  • 1
    Hmm...come to think of it, I can just use a label and throw some css on it to make it look like a link too. –  May 05 '09 at 17:46
  • Yep. That's the best and easiest solution. Some ClickHandlers on a Label are link like enough for most people. – bikesandcode Nov 19 '09 at 05:40

5 Answers5

5

Use Anchor instead.

As java doc of Hyperlink says:

If you want an HTML hyperlink ( tag) without interacting with the history system, use Anchor instead.

Eduard Wirch
  • 9,785
  • 9
  • 61
  • 73
1

The easiest way by far to do this is to create your own class that extends Composite, and just renders an <a> </a> as a HTML widget. I had this same problem and that's how I got around it.

rustyshelf
  • 44,963
  • 37
  • 98
  • 104
1

Like rustyshelf says,

final HTML contactLink = new HTML(""
          + contactName + "");
contactLink.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
  .....
 }
}

http://gwt.google.com/samples/Showcase/Showcase.html#CwStackPanel

1

Just remove href="" from Anchor.

<g:Anchor ui:field="clickField">Click here</g:Anchor>
Sree
  • 746
  • 6
  • 21
0

I created this class

import com.google.gwt.user.client.ui.HTML;

public class HyperlinkWithoutHistoryToken extends HTML {
    public HyperlinkWithoutHistoryToken(String html, boolean wordWrap) {
        super("<a href=\"javascript:undefined;\">" + html + "</a>", wordWrap);
    }
    public HyperlinkWithoutHistoryToken(String html) {
    super("<a href=\"javascript:undefined;\">" + html + "</a>");
    }
}
raisercostin
  • 8,777
  • 5
  • 67
  • 76