Using the OnDOMReady Event
We recommend performing all JavaScript initialization within the LoadListener::OnDOMReady
event-- this is called when the page has finished parsing the document and has loaded the DOM into memory.
Scripts may execute before this event!
Some scripts on the page may execute before
OnDOMReady
is called. If you need to initialize JavaScript on the page before any scripts are executed, you should useLoadListener::OnWindowObjectReady
instead.Why don't we just always use OnWindowObjectReady?
The caveat of using
OnWindowObjectReady
is that it is only called on pages that have scripts on them, also you can't do any DOM manipulation in that event since the DOM may be not be fully loaded yet.
Attaching with the LoadListener interface
This event is part of the LoadListener
interface, we will inherit from it and bind it to our View
.
#include <Ultralight/Ultralight.h>
using namespace ultralight;
class MyListener : public LoadListener {
public:
MyListener(View* view) {
view->set_load_listener(this);
}
virtual ~MyListener() {}
///
/// Use LoadListener::OnDOMReady to wait for the DOM to load.
///
virtual void OnDOMReady(View* caller,
uint64_t frame_id,
bool is_main_frame,
const String& url) override {
///
/// Ignore DOMReady events from child frames.
///
if (!is_main_frame)
return;
///
/// The main document has loaded, we can initialize any DOM elements and / or
/// set up our JavaScript bindings here.
///
}
};
Updated 7 months ago