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 is incomplete. In contrast,OnDOMReady
is always called, just after the DOM has loaded.
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 {
///
/// Initialize the page here with JavaScript.
///
}
};
Updated over 1 year ago