An WAnchor is a widget that represents an HTML
<a>
element, and provides a link to a URL. So, you can
use an anchor to provide link to another web page, document, internal
application path or a resource. The anchor may contain a label text, an
image, or any other widget (as it inherits from
WContainerWidget).
void AnchorImage() {
WLink link = new WLink("https://www.emweb.be/");
link.setTarget(LinkTarget.NewWindow);
WAnchor anchor = new WAnchor(link);
new WImage(new WLink("https://www.emweb.be/css/emweb_small.png"), (WContainerWidget) anchor);
}
When an anchor is activated, by default the browser will replace the JWt
application with the targeted document or external url. This could
terminate the application. This may be changed to suggest the browser to
follow the link in a new window, using the setTarget()
method
with parameter JWt.TargetNewWindow
.
Even for non-HTML documents, this may be important since pending Ajax
requests are cancelled if documents are not served within the browser
window in certain browsers.
WAnchor
plays an important role for navigation within your
application, using JWt's internal paths, since they provide support for
bookmarks, the browser back/forward buttons, and following links in new
windows. For example, the WMenu widget
(used here to navigate JWt widgets) uses anchors for its items by default.
You may specify the anchor's target URL directly, but anchors can also refer to a WResource. A resource specifies application-dependent content that may be generated by your application on demand. This allows you to serve auxiliary files related to a particular application session, and perhaps dynamically generate the content. JWt includes WFileResource to stream a file and WMemoryResource to stream a data vector. When linking to a resource, the anchor does not assume ownership of the resource. As a result, you may share the same resources for several anchors.
Note that if you set a text or image using one of the API methods like
setText()
or setImage()
or a constructor, you should
not attempt to remove all contents (using clear()
, or provide a
layout (using setLayout()
), as this will result in undefined
behaviour. The text or image are simply inserted as widgets into the
container.
The widget corresponds to the HTML <a>
tag and does not
provide styling. It can be styled using inline or external CSS as
appropriate.