I'm trying to create a link (anchor) in GWT that can be clicked and it's ClickEvent
can be processed, while at the same time this anchor wouldn't reload the page. This basically means that a href
must not be set.
In javascript this is done like this:
<a href="javascript:handleAnchorClick();">Link</a>
or with
<a href="#" onclick="return handleAnchorClick()">Link</a>
where handleAnchorClick()
returns false.
What would be the best way to achieve this in GWT?
Yes, it is valid to use the anchor tag without a href attribute. If the a element has no href attribute, then the element represents a placeholder for where a link might otherwise have been placed, if it had been relevant, consisting of just the element's contents.
The tag is fine to use without an href attribute. Contrary to many of the answers here, there are actually standard reasons for creating an anchor when there is no href. Semantically, "a" means an anchor or a link. If you use it for anything following that meaning, then you are fine.
To make an anchor tag refer to nothing, use “javascript: void(0)”. The following link does nothing because the expression "0" has no effect in JavaScript. Here the expression "0" is evaluated, but it is not loaded back into the current document.
(Hypertext REFerence) The HTML code used to create a link to another page. The HREF is an attribute of the anchor tag, which is also used to identify sections within a document.
Use tha Anchor element, and call its addClickListener() method and add in what ever logic you wish. This sort of anchor doesn't reload the page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With