---
updatedAt: 2025-06-16T21:15:15.000Z
---

Fetch the complete documentation index at: https://docs.ultralig.ht/llms.txt. Use this file to discover all available pages before exploring further.

# Using the OnDOMReady Event

We recommend performing all JavaScript initialization within the [`LoadListener::OnDOMReady`](https://ultralig.ht/api/cpp/1_4_0/classultralight_1_1_load_listener.html#a5475cd002471c4325576bb71f2713c11) 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 use [`LoadListener::OnWindowObjectReady`](https://ultralig.ht/api/cpp/1_4_0/classultralight_1_1_load_listener.html#aedc3f1dd86852a15e8d5ee067b67f471) 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`](https://ultralig.ht/api/cpp/1_4_0/classultralight_1_1_load_listener.html) interface, we will inherit from it and bind it to our `View`.

```cplusplus
#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.
    ///
  }
};
```