A set of DOM event listeners and lifecycle hooks that persists across page navigations.
You define DOM event listeners and setup hooks in a dom::Triggers set and attach it to a View instead of re-registering handlers on every page load. One set can serve several Views, and it functions even when JavaScript is disabled.
Attach the set to a View before loading content so every page gets the listeners:
ui.
On(
"#save",
"click", [] { SaveGame(); });
});
view->LoadURL("file:///app.html");
The root of a page's DOM tree in a View or frame.
Definition Document.h:106
Element getElementById(std::string_view id) const
Find the element with a certain id (getElementById).
Definition Document.h:190
detail::StringProp< detail::TextContentTag > textContent
The text of this element and all its descendants (textContent).
Definition Element.h:216
A set of DOM event listeners and lifecycle hooks that persists across page navigations.
Definition Triggers.h:253
bool AttachTo(View *view, const AttachOptions &options={})
Attach this set to a View.
Definition Triggers.h:472
void OnDOMReady(F &&callback)
Add a hook that runs on each page that gets the set, once its document has finished parsing.
Definition Triggers.h:421
bool On(std::string_view selector, std::string_view type, F &&callback, EventListenerFlags flags=EventListenerFlags::None)
Add a listener for events on elements matching a CSS selector.
Definition Triggers.h:338
Delivery Timing
Pages receive an attached set based on their load state:
- A loading page gets the set once its document finishes parsing. Any OnDOMReady() hooks run right after the listeners attach.
- Attaching after a page has loaded applies the listeners right away. Any OnDOMReady() hooks run synchronously inside AttachTo().
- Handlers added after attaching reach only subsequent pages. The current page keeps its existing listeners unchanged.
- Note
- The page's own DOMContentLoaded event fires before the set arrives. Use OnDOMReady() for setup code that runs once document parsing completes.
Origin Rules and Page Filtering
By default, only your own content gets the set (file:// pages and View::LoadHTML()). Passing custom origin rules in AttachOptions replaces this default policy:
- Local pages require an explicit "file://*" rule. Without it, local content stops receiving the set.
- Pages loaded without a URL match no origin rule. Content loaded through View::LoadHTML() without a URL has an opaque origin, so origin patterns never match it.
- Subframes are excluded unless requested. Pass dom::AllFrames in flags to deliver listeners to subframe documents and run OnDOMReady() for each frame's document.
- Malformed rules cancel the attachment. If any rule fails to parse, AttachTo() returns false, logs a warning explaining the failure, and leaves the View unchanged.
Configure origin rules and frame targeting through AttachOptions when attaching the set:
bool attached = ui.
AttachTo(view.get(), {
.flags = dom::AllFrames,
.origin_rules = { "https://*.mygame.com", "file://*" } });
For decisions origin rules can't express (such as checking a user setting or allowing an opaque origin), see dom::SetInjectionFilter() and dom::InjectionRequest.
Object Lifetime and Cleanup
Calling DetachFrom() removes the whole set from a View immediately, though individual registrations can't be removed on their own.
Destroying a dom::Triggers instance automatically detaches it from every View and stops its listeners right away. Storing the set as a member of the class whose methods its callbacks invoke ensures the listeners never outlive the surrounding object:
class Hud {
public:
explicit Hud(
View* view) {
ui_.On(
"#close",
"click", [
this] {
Dismiss(); });
if (!ui_.AttachTo(view))
Log("an origin rule failed to parse");
}
private:
dom::Triggers ui_;
};
Web-page container rendered to an offscreen surface.
Definition View.h:483
Dismiss
Auto-dismiss policy for floating panels and popup windows.
Definition Options.h:27
Back-Forward Cache Restores
When a page returns from the back-forward cache (Config::page_cache_size above 0), the library restores its event listeners before the pageshow event fires. Hooks registered with OnDOMReady() don't run again because the document was parsed during the initial load.
Register an OnRestore() hook for tasks that must run every time a cached page reappears:
});
void OnRestore(F &&callback)
Add a hook that runs each time a page that gets the set is restored from the back-forward cache (Conf...
Definition Triggers.h:440
Choosing Between Triggers and addEventListener
Choose between dom::Triggers and Element::addEventListener() based on whether the listeners belong to one page or the whole View:
- dom::Triggers persists across navigations and cache restores. Use it for interface elements that must remain active across page loads or multiple Views.
- Element::addEventListener() attaches to a single page's elements. Direct listeners belong to one document and are gone after a back-forward restore or navigation.
- See also
- dom::AttachOptions, dom::SetInjectionFilter(), dom::InjectionRequest, ultralight::OriginRules, dom::Element::On(), dom::EventListener
|
| | Triggers () |
| | Create a new set of DOM listeners.
|
| | Triggers (const Triggers &)=delete |
| Triggers & | operator= (const Triggers &)=delete |
| | Triggers (Triggers &&other) noexcept |
| | Move constructor (other becomes empty).
|
| Triggers & | operator= (Triggers &&other) noexcept |
| | Move assignment (releases the set this object held, then takes over other's).
|
| | ~Triggers () |
| | Release this object's reference to the set.
|
| | operator bool () const |
| | Whether or not this object holds a set (false after a move or LeakRef(), or when it wraps NULL).
|
| template<typename F> |
| bool | On (std::string_view selector, std::string_view type, F &&callback, EventListenerFlags flags=EventListenerFlags::None) |
| | Add a listener for events on elements matching a CSS selector.
|
template<typename C, typename M>
requires (std::is_class_v<C> && std::is_member_function_pointer_v<M> && !LockableHolder<C>) |
| bool | On (std::string_view selector, std::string_view type, C &receiver, M method, EventListenerFlags flags=EventListenerFlags::None) |
| | Add a selector listener that calls a member function on a borrowed receiver (no forwarding lambda needed):
|
template<typename H, typename M>
requires (LockableHolder<H> && std::is_member_function_pointer_v<M>) |
| bool | On (std::string_view selector, std::string_view type, H holder, M method, EventListenerFlags flags=EventListenerFlags::None) |
| | Add a selector listener that calls a member function through a holder (a shared_ptr, a weak_ptr, or a custom smart pointer with a HolderTraits specialization).
|
| template<typename F> |
| void | OnDOMReady (F &&callback) |
| | Add a hook that runs on each page that gets the set, once its document has finished parsing.
|
| template<typename F> |
| void | OnRestore (F &&callback) |
| | Add a hook that runs each time a page that gets the set is restored from the back-forward cache (Config::page_cache_size > 0).
|
| bool | AttachTo (View *view, const AttachOptions &options={}) |
| | Attach this set to a View.
|
| void | DetachFrom (View *view) |
| | Detach this set from a View.
|
| ULDOMTriggers | raw () const |
| | Get the C handle, for passing to the <Ultralight/CAPI/CAPI_DOMTriggers.h> functions.
|
| ULDOMTriggers | LeakRef () |
| | Give up ownership of the C handle and return it.
|