tags:

views:

356

answers:

3

I want to handle the event in the case of link by my own event listener.If we click on a link in browser, browser will open the address given in the link but i want to call my own event listener. I tried to do it in GWT by removing the attribute of the anchor tag which worked but it is not a clean solution. So if you are having any idea how to block the browser from opening that link please reply.

A: 

Isn't that what this widget does:

http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/user/client/ui/Hyperlink.html

eg: it looks like a normal hyperlink but lets you handle the onclick event?

rustyshelf
This answer makes a good point. Look into GWT's History support, it may be exactly what you need.http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/user/client/History.html
Mark Renouf
A: 

You can just call event.cancel() (GWT 1.6) or

Event.getCurrentEvent().cancelBubble(true);              // In 1.4 and earlier
DOM.eventCancelBubble(DOM.eventGetCurrentEvent(), true); // In 1.5

There is also a method to cancel an event from its instance within GWT 1.5, but I can't remember.

Miguel Ping
+1  A: 

In GWT 1.6 the correct code is:

ClickHandler foo = new ClickHandler() { 
    public void onClick(ClickEvent event) {

        /// do your stuff

        event.stopPropagation(); // stops the event from bubbling to parent

        event.preventDefault();  // prevents the browsers default action,
                                 // following a link, etc
    }
}

This is roughly equivalent to:

<a href="#" onclick="return false">Link</a>
Mark Renouf