Anchor

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).

Example Wt homepage (in a new window)
source
#include <Wt/WAnchor.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WLink.h>


// Create an anchor that links to a URL through clickable text.
Wt::WLink link = Wt::WLink("https://www.webtoolkit.eu/");
link.setTarget(Wt::LinkTarget::NewWindow);

std::unique_ptr<Wt::WAnchor> anchor =
        std::make_unique<Wt::WAnchor>(link,
                        "Wt homepage (in a new window)");
Example
source
#include <Wt/WAnchor.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WImage.h>
#include <Wt/WLink.h>


// Create an anchor that links to a URL through an image.
Wt::WLink link = Wt::WLink("https://www.emweb.be/");
link.setTarget(Wt::LinkTarget::NewWindow);

std::unique_ptr<Wt::WAnchor> anchor = std::make_unique<Wt::WAnchor>(link);
anchor->addNew<Wt::WImage>(Wt::WLink("https://www.emweb.be/css/emweb_small.png"));

When an anchor is activated, by default the browser will replace the Wt 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 Wt::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 Wt'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 Wt 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. Wt 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.

Top